Finding Leaks with memcheck
Out-of-bounds and bad device pointers.
Finding Leaks with memcheck is a free CUDA Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why memcheck Exists
GPU memory bugs rarely crash loudly, so compute-sanitizer with the memcheck tool watches every access and reports the trouble for you. 🛡️
The Modern Command
Run your program under the memcheck tool by prefixing it with compute-sanitizer, the modern replacement for the old cuda-memcheck.
compute-sanitizer --tool memcheck ./vecaddOut-of-Bounds Reads
The classic bug memcheck catches is an out-of-bounds access, where a thread reads or writes past the end of a device array.
A Missing Bounds Check
Forgetting the guard lets extra threads run wild, so this index can stray beyond n and trigger an invalid global write.
int i = blockIdx.x * blockDim.x + threadIdx.x;
out[i] = a[i] + b[i];Reading the Report
memcheck prints the error kind, the offending address, and the thread that caused it, pointing you straight at the bad access.
Get Line Numbers
Compile with -lineinfo so memcheck can map each error back to the exact source line instead of a raw address.
nvcc -lineinfo vecadd.cu -o vecaddDetecting Leaks
Turn on the --leak-check option and memcheck reports any device memory you allocated but never released before exit.
compute-sanitizer --leak-check full ./vecaddA Classic Leak
Allocating without a matching free leaks the buffer, and across many iterations that slowly exhausts device memory.
cudaMalloc(&d_a, bytes);
// ...used, but cudaFree(d_a) is missingInvalid Device Pointers
memcheck also flags passing a host pointer where the kernel expects a device pointer, a mistake that silently corrupts results.
Misaligned Access
Some loads require aligned addresses, so memcheck warns about a misaligned access that would otherwise produce garbage or a fault.
Fix and Rerun
After each fix, run memcheck again until it reports zero errors. A clean run is your sign the memory bugs are gone.
Quick Check
What kind of bug is the memcheck tool primarily designed to find?
Recap
You ran memcheck, added -lineinfo for source lines, enabled leak checking, and learned its common error kinds. Clean runs mean safe memory. ✅
Frequently asked questions
Is the “Finding Leaks with memcheck” lesson free?
Yes — the full text of “Finding Leaks with memcheck” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.
What will I learn in “Finding Leaks with memcheck”?
Out-of-bounds and bad device pointers. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CUDA Academy?
No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Finding Leaks with memcheck” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CUDA Academy lesson?
Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Stepping Kernels in cuda-gdb
- Finding Leaks with memcheck
- Hunting Races with racecheck
- Catching Sync Errors with synccheck