次の C++ ファイル pwd01.cpp があります。
#include <pwd.h>
#include <iostream>
int main() {
passwd* pwd = getpwuid(getuid());
}
これを次のコマンドでコンパイルします。
g++ pwd01.cpp -Wall -o pwd01
Ubuntu 12.04.1 LTS / gcc バージョン 4.6.3 では、valgrind がリークを報告します (以下を参照)。Mac OS 10.6.8 / gcc バージョン 4.2.1 で同じコマンドを使用して同じコードをコンパイルすると、valgrind はリークを報告しません。
passwd* を解放する必要がないことは承知しています(Linux で getpwuid() によって返されたポインターを解放する必要がありますか? )。それで、私は何が欠けていますか?
valgrind ./pwd01
==10618== Memcheck, a memory error detector
==10618== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==10618== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==10618== Command: ./pwd01
==10618==
==10618==
==10618== HEAP SUMMARY:
==10618== in use at exit: 300 bytes in 11 blocks
==10618== total heap usage: 68 allocs, 57 frees, 10,130 bytes allocated
==10618==
==10618== LEAK SUMMARY:
==10618== definitely lost: 60 bytes in 1 blocks
==10618== indirectly lost: 240 bytes in 10 blocks
==10618== possibly lost: 0 bytes in 0 blocks
==10618== still reachable: 0 bytes in 0 blocks
==10618== suppressed: 0 bytes in 0 blocks
==10618== Rerun with --leak-check=full to see details of leaked memory
==10618==
==10618== For counts of detected and suppressed errors, rerun with: -v
==10618== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)