Memory Leakage in ThirdParty C-Code/C-Library -
i have memory leakage in absolut frustrating third party static library...i narrow down following function "read_lut". think leakage because of p = (struct csoln*) malloc(ns*sizeof(struct csoln)); c very rusty when comes down memory handling... can me free memory correctly?
void readlut() { unsigned char charnum[256], line[32], *linep, c; file *fpwv, *fprt; struct csoln *p; int d, i, j, k, kk, ns, nn; init_param(); (i=0; i<=255; i++) { if ('0'<=i && i<='9') charnum[i] = - '0'; else if (i>='a') charnum[i] = - 'a' + 10; else // if (i=='$' || i=='\n' || ... ) charnum[i] = 0; } fpwv=fopen(powvfile, "r"); if (fpwv == null) { printf("error in opening %s\n", powvfile); exit(1); } #if routing==1 fprt=fopen(postfile, "r"); if (fprt == null) { printf("error in opening %s\n", postfile); exit(1); } #endif (d=4; d<=d; d++) { fscanf(fpwv, "d=%d\n", &d); #if routing==1 fscanf(fprt, "d=%d\n", &d); #endif (k=0; k<numgrp[d]; k++) { ns = (int) charnum[fgetc(fpwv)]; if (ns==0) { // same previous group fscanf(fpwv, "%d\n", &kk); numsoln[d][k] = numsoln[d][kk]; lut[d][k] = lut[d][kk]; } else { fgetc(fpwv); // '\n' numsoln[d][k] = ns; p = (struct csoln*) malloc(ns*sizeof(struct csoln)); lut[d][k] = p; (i=1; i<=ns; i++) { linep = (unsigned char *) fgets((char *) line, 32, fpwv); p->parent = charnum[*(linep++)]; j = 0; while ((p->seg[j++] = charnum[*(linep++)]) != 0) ; j = 10; while ((p->seg[j--] = charnum[*(linep++)]) != 0) ; #if routing==1 nn = 2*d-2; fread(line, 1, d-2, fprt); linep=line; (j=d; j<nn; j++) { c = charnum[*(linep++)]; p->rowcol[j-d] = c; } fread(line, 1, nn/2+1, fprt); linep=line; // last char \n (j=0; j<nn; ) { c = *(linep++); p->neighbor[j++] = c/16; p->neighbor[j++] = c%16; } #endif p++; } } } } }
lut defined globally as:
struct csoln { unsigned char parent; unsigned char seg[11]; // add: 0..i, sub: j..10; seg[i+1]=seg[j-1]=0 unsigned char rowcol[d-2]; // row = rowcol[]/16, col = rowcol[]%16, unsigned char neighbor[2*d-2]; }; struct csoln *lut[d+1][mgroup]; // storing 4 .. d
Comments
Post a Comment