Heim  >  Artikel  >  Backend-Entwicklung  >  Wie debugge ich C++-Speicherlecks mit Valgrind?

Wie debugge ich C++-Speicherlecks mit Valgrind?

WBOY
WBOYOriginal
2024-06-03 16:48:01908Durchsuche

Wie debugge ich C++-Speicherlecks mit Valgrind?

So verwenden Sie Valgrind zum Debuggen von C++-Speicherlecks

Valgrind ist ein leistungsstarker Speicherdebugger, mit dem Speicherlecks, illegale Verwendungen und Zuordnungsprobleme in C++-Programmen erkannt werden können. So verwenden Sie Valgrind zum Debuggen von C++-Speicherlecks:

1. Installieren Sie Valgrind

Verwenden Sie den folgenden Befehl, um Valgrind zu installieren:

sudo apt install valgrind

2. Fügen Sie beim Kompilieren des Programms hinzu -g Flag zum Generieren von Debugging-Informationen:

g++ -g my_program.cpp -o my_program

Verwenden Sie dann Valgrind, um das Programm auszuführen, und verwenden Sie das Flag --leak-check=full, um nach Speicherlecks zu suchen:

valgrind --leak-check=full ./my_program
-g 标记以生成调试信息:

#include <iostream>

int* leak() {
  int* ptr = new int;
  return ptr;
}

int main() {
  int* ptr = leak();
  return 0;
}

然后,使用 Valgrind 运行程序,并使用 --leak-check=full 标记来检查内存泄漏:

g++ -g leak.cpp -o leak
valgrind --leak-check=full ./leak

3. 分析 Valgrind 输出

Valgrind 的输出将包含有关检测到的内存泄漏的信息。

实战案例

以下是一个模拟内存泄漏的简单 C++ 程序:

==27244== Memcheck, a memory error detector
==27244== Copyright (C) 2002-2017, and GNU GPL'd by, Julian Seward et al.
==27244== Using Valgrind-3.15.0.
==27244== Command: ./leak
==27244==
==27244== HEAP SUMMARY:
==27244==     in use at exit: 4 bytes in 1 blocks
==27244==   total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==27244==
==27244== LEAK SUMMARY:
==27244==    definitely lost: 4 bytes in 1 blocks
==27244==    indirectly lost: 0 bytes in 0 blocks
==27244==      possibly lost: 0 bytes in 0 blocks
==27244==    still reachable: 0 bytes in 0 blocks
==27244==         suppressed: 0 bytes in 0 blocks
==27244== Rerun with --leak-check=full to see what's still reachable
==27244==
==27244== For counts of detected and suppressed errors, rerun with: -v
==27244== Use --track-origins=yes to see where unfreed memory was allocated
==27244== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==27244==
==27244== 1 errors in context 0 of 1:
==27244== Invalid read of size 8
==27244==    at 0x4842E10: leak (leak.cpp:5)
==27244==    by 0x483D8E7: main (leak.cpp:12)
==27244==  Address 0x555555555600 is not stack'd, malloc'd or (recently) free'd
==27244==
==27244== LEAK SUMMARY:
==27244==    definitely lost: 0 bytes in 0 blocks
==27244==    indirectly lost: 0 bytes in 0 blocks
==27244==      possibly lost: 4 bytes in 1 blocks
==27244==    still reachable: 0 bytes in 0 blocks
==27244==         suppressed: 0 bytes in 0 blocks
==27244== Rerun with --leak-check=full to see what's still reachable
==27244==
==27244== For counts of detected and suppressed errors, rerun with: -v
==27244== Use --track-origins=yes to see where unfreed memory was allocated

编译并使用 Valgrind 运行此程序:

rrreee

Valgrind 的输出将包含以下信息:

rrreee

此输出表明程序中存在 4 字节的内存泄漏,该泄漏来自函数 leak() 中未释放的 int

3. Valgrind-Ausgabe analysieren 🎜🎜🎜Die Ausgabe von Valgrind enthält Informationen über den erkannten Speicherverlust. 🎜🎜🎜Praktischer Fall🎜🎜🎜Das Folgende ist ein einfaches C++-Programm, das einen Speicherverlust simuliert: 🎜rrreee🎜Kompilieren Sie dieses Programm und führen Sie es mit Valgrind aus: 🎜rrreee🎜Die Ausgabe von Valgrind enthält die folgenden Informationen: 🎜rrreee🎜Diese Ausgabe Zeigt an, dass im Programm ein Speicherverlust vorliegt. 4-Byte-Speicherverlust durch einen nicht freigegebenen int-Zeiger in der Funktion leak(). 🎜

Das obige ist der detaillierte Inhalt vonWie debugge ich C++-Speicherlecks mit Valgrind?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn