Sometimes, it's because of forgetting to free some pointers when exiting a function with 'return'; and especially when the function is creating a lot of pointers and they need to be freed.
This way to use keyword 'goto' should solve the problem:
//---
void test_func(){
void* A;
void* B;
void* C;
A = malloc(1000);
B = malloc(1000);
C = malloc(1000);
// Some deep logic
if (1==1){
if (2==2){
// Do something
// ...
// Need to return? don't
// Go to the mem freeing at function tail
goto ret;
}
}
// Do other things here
// ...
ret:
free(A);
free(B);
free(C);
}
//---
No comments:
Post a Comment