2014-10-17 167 views
1

我正在做一個小型項目。我正在使用Valgrind工具檢查內存泄漏。當我使用這個工具時,我得到了下面的信息。在strdup中Valgrind內存泄漏

> 584 bytes in 74 blocks are definitely lost in loss record 103 of 104 
> ==4628== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
> ==4628== by 0x41CF8D0: strdup (strdup.c:43) 
> ==4628== by 0x8060B95: main (in mycall) 
> 
> LEAK SUMMARY: 
> ==4628== definitely lost: 584 bytes in 74 blocks 
> ==4628== indirectly lost: 0 bytes in 0 blocks 
> ==4628==  possibly lost: 0 bytes in 0 blocks 
> ==4628== still reachable: 21,414 bytes in 383 blocks 
> ==4628==   suppressed: 0 bytes in 0 blocks 
> ==4628== 
> ==4628== For counts of detected and suppressed errors, rerun with: -v 
> ==4628== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) 

這些是我使用函數strdup的編碼。我用過LEX代碼。

{string} { 

    yylval.string = strdup(yytext + 1); 
    yylval.string[yyleng - 2] = 0; 
    return PPSTRING; 
    } 

{numvar}  { yylval.string = strdup(yytext);return(PPNUMVAR);  } 
{sysnumvar}  { yylval.string = (char *) strdup(yytext);return(PPSYSNUMVAR); } 

我不知道內存在哪一點泄漏。

回答

2

strdup功能分配必要的內存來存儲採購串隱含,則需要手動(即yylval.string在你的代碼)釋放返回的字符串。