누설 :realloc을() I 문자열에 문자를 추가하는 기능을 가지고 메모리
void AddChToString(char **str,char ch){
int len=(*str)?strlen(*str):0;
(*str)=realloc(*str, len+2);
(*str)[len]=ch;
(*str)[len+1]='\0';
}
하고 (Mac) 인스트루먼트 Valgrind의를 표시하는 선이 (* STR) = realloc을 (* STR , len + 2)에 메모리 누수가 있습니다. 이 realloc 구현 문제가 있습니까? 아니면 부적절하게 사용하고 있습니까?
==39230== 6 bytes in 1 blocks are definitely lost in loss record 1 of 7
==39230== at 0x100018B2D: realloc (vg_replace_malloc.c:525)
==39230== by 0x100002259: AddChToString (in ./OpenOtter)
==39230== by 0x10000477B: QueryMapFromString (in ./OpenOtter)
==39230== by 0x100684CD2: ???
==39230== by 0x100001FB0: RequestHandler (in ./OpenOtter)
==39230== by 0x100065535: _pthread_start (in /usr/lib/libSystem.B.dylib)
==39230== by 0x1000653E8: thread_start (in /usr/lib/libSystem.B.dylib)
==39230==
==39230== 9 bytes in 1 blocks are definitely lost in loss record 2 of 7
==39230== at 0x100018B2D: realloc (vg_replace_malloc.c:525)
==39230== by 0x100002259: AddChToString (in ./OpenOtter)
==39230== by 0x10000298E: ParseHTTPRequest (in ./OpenOtter)
==39230== by 0x100004151: OpenRoutesFile (in ./OpenOtter)
==39230== by 0x10000142B: main (in ./OpenOtter)
==39230==
==39230== 45 bytes in 5 blocks are definitely lost in loss record 3 of 7
==39230== at 0x100018B2D: realloc (vg_replace_malloc.c:525)
==39230== by 0x100002259: AddChToString (in ./OpenOtter)
==39230== by 0x10000298E: ParseHTTPRequest (in ./OpenOtter)
==39230== by 0x100001EB4: RequestHandler (in ./OpenOtter)
==39230== by 0x100065535: _pthread_start (in /usr/lib/libSystem.B.dylib)
==39230== by 0x1000653E8: thread_start (in /usr/lib/libSystem.B.dylib)
==39230==
==39230== LEAK SUMMARY:
==39230== definitely lost: 60 bytes in 7 blocks
==39230== indirectly lost: 0 bytes in 0 blocks
==39230== possibly lost: 0 bytes in 0 blocks
==39230== still reachable: 1,440 bytes in 4 blocks
==39230== suppressed: 0 bytes in 0 blocks
감사 : 여기서
는 Valgrind의에서 출력된다.
을 어쨌든 첫 번째 간접를 사용하지 않는 경우 포인터가 포인터하는 이유는 무엇입니까? – delnan
어떤 악기를 사용하고 있습니까? 가상 메모리 크기를 보여주는 것을 사용한다면, realloc()이 반환하는 경우, 문제가되지 않는다. (물론, 적어도 'realloc()'... 그 코드와 관련된 문제는 아님) – Anon
NULL, 메모리 포인터를 잃어 버린다. – Nyan