#c
❓
How to read Stack Trace / Valgrind Report to find memory leaks?Quyida example valgrind report berilgan:
https://pastebin.com/EvMEDjrSXo'sh, uni o'qib nimani tushunish mumkin? Here is my example:
0x483EB26 joylashuvida background memory allocation amalga oshmoqda. Bunga strdup() funksiyasini response.c faylimizning 17-qatorida httpresponse_constructor() ichida chaqirish sabab bo'lyapti. httpresponse_constructor() esa server.c faylning 212-qatorida request_handler() tomonidan, u esa server.c faylining 56-qatorida launch() tomonidan, u esa main.c faylining 31-qatorida main() funksiyasi yordamida chaqirilmoqda.
Okay, demak men shu yerda qilgan orqa fondagi mallocimni free qilmay ketibman:
HTTPResponse *httpresponse_constructor()
{
HTTPResponse *res = malloc(sizeof(HTTPResponse));
if (!res) return NULL;
res->status_code = 200;
res->version = strdup("HTTP/1.1");
res->reason_phrase = strdup("OK");
res->headers = NULL;
res->header_count = 0;
res->body = NULL;
res->body_length = 0;
return res;
}
🤔 Buni endi qanday free qilamiz? Ownership kimdan kimga o'tyapti:
- httpresponse_constructor() dan request_handler() ga;
- request_handler() dan launch() ga.
Demak, launch() funksiyasi uni free() qilishga javobgar, demak free() shu yerda amalga oshiramiz.
@voidplog