Cómo encontrar fugas con memcheck
Accesos fuera de límites y punteros de dispositivo incorrectos
Cómo encontrar fugas con memcheck es una lección gratuita de CUDA Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de CUDA Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de CUDA Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. ✅
Preguntas frecuentes
¿La lección «Cómo encontrar fugas con memcheck» es gratis?
Sí — el texto completo de «Cómo encontrar fugas con memcheck» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de CUDA Academy, actualiza a CoddyKit PRO. El curso de CUDA Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Cómo encontrar fugas con memcheck»?
Accesos fuera de límites y punteros de dispositivo incorrectos Practicas CUDA Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar CUDA Academy?
No se requiere experiencia previa. CUDA Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Cómo encontrar fugas con memcheck»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de CUDA Academy?
Sí. Cada lección de CUDA Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Depuración paso a paso de kernels con cuda-gdb
- Cómo encontrar fugas con memcheck
- Cómo detectar condiciones de carrera con racecheck
- Cómo detectar errores de sincronización con synccheck