memcheck でリークを見つける
範囲外アクセスと不正なデバイスポインタ
「memcheck でリークを見つける」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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 ./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. ✅
よくある質問
「memcheck でリークを見つける」レッスンは無料ですか?
はい。「memcheck でリークを見つける」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。
「memcheck でリークを見つける」で何を学びますか?
範囲外アクセスと不正なデバイスポインタ ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
CUDA Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「memcheck でリークを見つける」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCUDA Academyレッスンでコードを書いて実行できますか?
はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- cuda-gdb でカーネルをステップ実行
- memcheck でリークを見つける
- racecheck で競合を探す
- synccheck で同期エラーを検出