{"id":20619025,"url":"https://github.com/tessapower/mem-leak-playground","last_synced_at":"2026-05-31T02:31:44.779Z","repository":{"id":188839763,"uuid":"472547330","full_name":"tessapower/mem-leak-playground","owner":"tessapower","description":"A playground to test out memory leaks and how to find them using the tools Top and Valgrind.","archived":false,"fork":false,"pushed_at":"2026-02-14T23:30:18.000Z","size":282,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-15T06:47:10.663Z","etag":null,"topics":["cpp","memory-leak-detection","valgrind"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tessapower.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-03-21T23:32:19.000Z","updated_at":"2026-02-14T23:30:22.000Z","dependencies_parsed_at":"2023-08-17T05:12:04.525Z","dependency_job_id":null,"html_url":"https://github.com/tessapower/mem-leak-playground","commit_stats":null,"previous_names":["tessapower/mem-leak-playground"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tessapower/mem-leak-playground","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fmem-leak-playground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fmem-leak-playground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fmem-leak-playground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fmem-leak-playground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tessapower","download_url":"https://codeload.github.com/tessapower/mem-leak-playground/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fmem-leak-playground/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33717415,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-31T02:00:06.040Z","response_time":95,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","memory-leak-detection","valgrind"],"created_at":"2024-11-16T12:10:14.829Z","updated_at":"2026-05-31T02:31:44.771Z","avatar_url":"https://github.com/tessapower.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Memory Leak Playground\r\n\r\nThese are my personal notes for myself to help with future debugging. Every `new` or `malloc`  in C++ requires a `delete`/`delete[]` or `free`. Not deallocating allocated stack memory results in memory leaks.\r\n\r\n## TL;DR\r\n\r\n1. Build: `cmake -B build \u0026\u0026 cmake --build build`\r\n2. Run a leak example: `./build/app/mem_leak missing_delete`\r\n3. Run `Valgrind` against it: `valgrind --leak-check=full ./build/app/mem_leak missing_delete`\r\n4. Or run all examples at once: `valgrind --leak-check=full ./build/app/mem_leak all`\r\n5. Use `Top` to watch memory usage: `top -p $(pgrep mem_leak)`\r\n\r\n## Leak Examples\r\n\r\nThe app includes 5 runnable examples, one per common leak category:\r\n\r\n| Command | What leaks | Why |\r\n|---|---|---|\r\n| `missing_delete` | `new int[100]` | No matching `delete[]` |\r\n| `lost_pointer` | `new int(1)` | Pointer reassigned before freeing the original |\r\n| `error_path` | `new int[50]` | Early return skips `delete[]` |\r\n| `container` | 5x `new int` in a `vector\u003cint*\u003e` | `vector::clear()` destroys pointers, not pointees |\r\n| `circular` | Two `shared_ptr\u003cNode\u003e` | Circular references keep ref counts above zero |\r\n\r\nRun a single example to isolate one leak type in Valgrind output:\r\n\r\n```shell\r\nvalgrind --leak-check=full ./build/app/mem_leak error_path\r\n```\r\n\r\nOr run them all:\r\n\r\n```shell\r\nvalgrind --leak-check=full ./build/app/mem_leak all\r\n```\r\n\r\nRun with no arguments to see usage:\r\n\r\n```shell\r\n./build/app/mem_leak\r\n```\r\n\r\n## `Valgrind`\r\n\r\n`Valgrind` displays memory leaks in programs. Running `valgrind FILE` shows how much memory, if any, was leaked:\r\n\r\n![Valgrind displaying memory leak](/docs/valgrind.png)\r\n\r\nRunning `Valgrind` with `--leak-check=full` reveals the place where the memory leak may be happening and the stack trace:\r\n\r\n![Valgrind displaying culprit and stack trace](/docs/valgrind-leak-check.png)\r\n\r\nAfter fixing the leak, running `Valgrind` again displays the happy news:\r\n\r\n![Valgrind no leaks](/docs/valgrind-no-leak.png)\r\n\r\n## `Top`\r\n\r\n`Top` displays Linux processes, but it's not so helpful to run by itself - processes shift around and it's hard to keep track of the one you want:\r\n\r\n![Top displaying Linux Processes](/docs/top.png)\r\n\r\nWe can use the `-p` switch and pass `Top` the PID to watch just our app running:\r\n\r\n![Top displaying just one](/docs/top-p.png)\r\n\r\nFinding the process each time is repetitive, so we can filter out the PID from the list of processes using:\r\n\r\n```shell\r\nps -aux | grep mem_leak | head -n 1 | awk '{print $2}'\r\n```\r\n\r\nTogether with the previous command we get:\r\n\r\n```shell\r\ntop -p $(ps -aux | grep mem_leak | head -n 1 | awk '{print $2}')\r\n```\r\n\r\nWhich displays the memory usage of the app with the new PID each time `Top` is run. Now press `E` to see the memory displayed in different formats, e.g. MB, GB, TB, etc.\r\n\r\n![Top displaying different memory format](/docs/top-p-e.png)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftessapower%2Fmem-leak-playground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftessapower%2Fmem-leak-playground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftessapower%2Fmem-leak-playground/lists"}