0Pricing
CUDA Academy · درس

العثور على تسرّبات الذاكرة باستخدام memcheck

الوصول خارج الحدود ومؤشرات الجهاز غير الصالحة

العثور على تسرّبات الذاكرة باستخدام memcheck درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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 ./vecadd

Out-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 vecadd

Detecting 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 ./vecadd

A 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 missing

Invalid 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. ✅

الأسئلة الشائعة

هل درس «العثور على تسرّبات الذاكرة باستخدام memcheck» مجاني؟

نعم — نص درس «العثور على تسرّبات الذاكرة باستخدام memcheck» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.

ماذا ستتعلم في «العثور على تسرّبات الذاكرة باستخدام memcheck»؟

الوصول خارج الحدود ومؤشرات الجهاز غير الصالحة تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟

لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «العثور على تسرّبات الذاكرة باستخدام memcheck»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟

نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. التنقّل خطوة بخطوة في النوى باستخدام cuda-gdb
  2. العثور على تسرّبات الذاكرة باستخدام memcheck
  3. اكتشاف حالات التسابق باستخدام racecheck
  4. اكتشاف أخطاء المزامنة باستخدام synccheck
← العودة إلى CUDA Academy