Have the following simple code: void test() { try { char *p = malloc(100); if (p == NULL) throw 1; } catch(int x) { if (x) printf("\n error"); } //dostuff(p); If I run cppcheck, it indicates no memory leaks which is incorrect as "p" variable is never allocated. However, if I comment out the try catch block, cppcheck works correct;ly. If I run the same sample without try-catch block, it shows error: Memory leak: p[memleak]
Thank you. Some additional information. Looks like if I add an extra if else loop so the code looks like below: int main() { char p = malloc(10); int x = 10; if (x > 0 ) { char q = malloc(10); strcpy(q,"Hello"); printf("\n %s",q); //free(q); } else { char q1 = malloc(10); strcpy(q1,"Hello"); printf("\n %s",q1); //free(q1); } if (x < 0 ) { char q = malloc(10); strcpy(q,"Hello"); printf("\n %s",q); //free(q); } else { char *q1 = malloc(10); strcpy(q1,"Hello"); printf("\n %s",q1); //free(q1); } return...
Hello; I submitted the following code to cppcheck. int main() { char p = malloc(10); int x = 10; if (x > 0 ) { char q = malloc(10); strcpy(q,"Hello"); printf("\n %s",q); //free(q); } else { char *q1 = malloc(10); strcpy(q1,"Hello"); printf("\n %s",q1); //free(q1); } return 0; } ================= cppcheck 2.3 just shows memory leak of variable p but not of q or q1. possible bug? Best, RK