Changeset: f68c1104bcfa for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=f68c1104bcfa
Modified Files:
        geom/monetdb5/geomPoints.c
Branch: geo
Log Message:

fixed memory leak


diffs (truncated from 545 to 300 lines):

diff --git a/geom/monetdb5/geomPoints.c b/geom/monetdb5/geomPoints.c
--- a/geom/monetdb5/geomPoints.c
+++ b/geom/monetdb5/geomPoints.c
@@ -44,9 +44,13 @@ static pbsm_info *limits = NULL;
 
 //hard coded filename
 static char* filename = "../pbsmIndex_20m";
-static char* idxFilename;
-static char* dataFilename;
-static char* limitsFilename;
+static char* idxEnding = ".idx";
+static char* dataEnding = ".data";
+static char* limitsEnding =".info";
+
+//static char* idxFilename;
+//static char* dataFilename;
+//static char* limitsFilename;
 
 //it gets two BATs with x,y coordinates and returns a new BAT with the points
 static BAT* BATMakePoint2D(BAT* xBAT, BAT* yBAT) {
@@ -895,200 +899,221 @@ static str wkbPointsFilterWithImprints_g
 
 
 /* PBSM */
-static str createFilenames(str module) {
-       char* idxEnding = ".idx";
-       char* dataEnding = ".data";
-       char* limitsEnding =".info";
-
-       //allocate space for the files
-       if((idxFilename = (char*) 
GDKmalloc(strlen(filename)+strlen(idxEnding)+1)) == NULL) {
-               return createException(MAL, module, "Problem allocating space 
for idxFilename");
-       }
-       if((dataFilename = (char*) 
GDKmalloc(strlen(filename)+strlen(dataEnding)+1)) == NULL) {
-               GDKfree(idxFilename);
-               return createException(MAL, module, "Problem allocating space 
for dataFilename");
-       }
-       if((limitsFilename = (char*) 
GDKmalloc(strlen(filename)+strlen(limitsEnding)+1)) == NULL) {
-               GDKfree(idxFilename);
-               GDKfree(dataFilename);
-               return createException(MAL, module, "Problem allocating space 
for limitsFilename");
-       }
-
-       strcpy(idxFilename, filename);
-       strcpy(idxFilename+strlen(filename), idxEnding);
-       strcpy(dataFilename, filename);
-       strcpy(dataFilename+strlen(filename), dataEnding);
-       strcpy(limitsFilename, filename);
-       strcpy(limitsFilename+strlen(filename), limitsEnding);
-
-       return MAL_SUCCEED;
-}
-
-static str allocateStructs(unsigned int numberOfOIDs) {
-       unsigned short i=0;
-
-       if ((pbsm_idx = (pbsm_ptr*)GDKmalloc(USHRT_MAX * sizeof(pbsm_ptr))) == 
NULL)
-               return createException(MAL, " geomPoints.c:allocateStructs", " 
Problem allocating space for pbsm_idx");
-
-       for (i = 0; i < USHRT_MAX; i++) {
-               pbsm_idx[i].count = 0;
-               pbsm_idx[i].offset = 0;
-       }
-
-       if ((oids = (oid*)GDKmalloc(numberOfOIDs * sizeof(oid))) == NULL) {
-               GDKfree(pbsm_idx);
-               pbsm_idx = NULL;
-               return createException(MAL, " geomPoints.c:allocateStructs", " 
Problem allocating space for oids");
-       }
-
-       if ((limits = (pbsm_info*)GDKmalloc(sizeof(pbsm_info))) == NULL) {
-               GDKfree(pbsm_idx);
-               pbsm_idx = NULL;
-               GDKfree(oids);
-               oids = NULL;
-               return createException(MAL, " geomPoints.c:allocateStructs", " 
Problem allocating space for limits");
-       }
-
-       return MAL_SUCCEED;
-}
 
 static str store(void) {
        FILE *f;
        str ret = MAL_SUCCEED;
+       char *idxFilename = NULL, *dataFilename = NULL, *infoFilename = NULL;
+
+       if((infoFilename = (char*) 
GDKmalloc(strlen(filename)+strlen(limitsEnding)+1)) == NULL)
+               return createException(MAL, " geomPoints.c:store", " Problem 
allocating space for infoFilename");
+       strcpy(infoFilename, filename);
+       strcpy(infoFilename+strlen(filename), limitsEnding);
+
+       if ((f = fopen(infoFilename, "wb"))) {
+               if (fwrite(limits, sizeof(*limits), 1, f) != 1) {
+                       fclose(f);
+                       ret =  createException(IO, " geomPoints.c:store", " 
Could not save the PBSM index to disk (target: %s)", infoFilename);
+                       GDKfree(infoFilename);
+                       infoFilename = NULL;
+                       return ret;
+               }
+                fflush(f);
+                fclose(f);
+               GDKfree(infoFilename);
+               infoFilename = NULL;
+
+       } else {
+               ret = createException(IO, " geomPoints.c:store", " Could not 
open for writting (target: %s)", infoFilename);
+               
+               GDKfree(infoFilename);
+               infoFilename = NULL;
+               return ret;
+       }
+
+       if((idxFilename = (char*) 
GDKmalloc(strlen(filename)+strlen(idxEnding)+1)) == NULL)
+               return createException(MAL, " geomPoints.c:store", " Problem 
allocating space for idxFilename");
+       strcpy(idxFilename, filename);
+       strcpy(idxFilename+strlen(filename), idxEnding);
 
        if ((f = fopen(idxFilename, "wb"))) {
                if (fwrite(pbsm_idx, sizeof(pbsm_idx[0]), USHRT_MAX,f) != 
USHRT_MAX) {
                        fclose(f);
                        ret = createException(IO, " geomPoints.c:store", " 
Could not save the PBSM index to disk (target: %s)", idxFilename);
-                       goto clean;
+                       GDKfree(idxFilename);
+                       idxFilename = NULL;
+                       return ret; 
                }
                 fflush(f);
                 fclose(f);
+
+               GDKfree(idxFilename);
+               idxFilename = NULL;
+
        } else {
                ret = createException(MAL, " geomPoints.c:store", " Could not 
open for writting (target: %s)", idxFilename);
-               goto clean;
+
+               GDKfree(idxFilename);
+               idxFilename = NULL;
+               return ret;             
        }
 
+       if((dataFilename = (char*) 
GDKmalloc(strlen(filename)+strlen(dataEnding)+1)) == NULL)
+               return createException(MAL, "geomPoints.c:store", " Problem 
allocating space for dataFilename");
+       strcpy(dataFilename, filename);
+       strcpy(dataFilename+strlen(filename), dataEnding);
+
        if ((f = fopen(dataFilename, "wb"))) {
                if (fwrite(oids, sizeof(oids[0]), limits->oidsNum, f) != 
limits->oidsNum) {
                        fclose(f);
                        ret = createException(IO, " geomPoints.c:store", " 
Could not save the PBSM index to disk (target: %s)", dataFilename);
-                       goto clean;
+                       GDKfree(dataFilename);
+                       dataFilename = NULL;
+                       return ret; 
                }
                 fflush(f);
                 fclose(f);
+
+               GDKfree(dataFilename);
+               dataFilename = NULL;
+
        } else {
                ret = createException(IO, " geomPoints.c:store", " Could not 
open for writting (target: %s)", dataFilename);
-               goto clean;
+
+               GDKfree(dataFilename);
+               dataFilename = NULL;
+               return ret; 
        }
-
        
-       if ((f = fopen(limitsFilename, "wb"))) {
-               if (fwrite(limits, sizeof(*limits), 1, f) != 1) {
-                       fclose(f);
-                       ret = createException(IO, " geomPoints.c:store", " 
Could not save the PBSM index to disk (target: %s)", limitsFilename);
-                       goto clean;
-               }
-                fflush(f);
-                fclose(f);
-       } else {
-               ret = createException(IO, " geomPoints.c:store", " Could not 
open for writting (target: %s)", limitsFilename);
-               goto clean;
-       }
-
-
-       GDKfree(idxFilename);
-       idxFilename = NULL;
-       GDKfree(dataFilename);
-       dataFilename = NULL;
-       GDKfree(limitsFilename);
-       limitsFilename = NULL;
-
        return MAL_SUCCEED;
-
-clean:
-       GDKfree(pbsm_idx);
-       pbsm_idx = NULL;
-       GDKfree(oids);
-       oids = NULL;
-       GDKfree(limits);
-       limits = NULL;
-       GDKfree(idxFilename);
-       idxFilename = NULL;
-       GDKfree(dataFilename);
-       dataFilename = NULL;
-       GDKfree(limitsFilename);
-       limitsFilename = NULL;
-
-       return ret;
 }
 
 static str load(void) {
        FILE *f;
+       char *idxFilename = NULL, *dataFilename = NULL, *infoFilename = NULL;
        str ret = MAL_SUCCEED;
 
+       if((infoFilename = (char*) 
GDKmalloc(strlen(filename)+strlen(limitsEnding)+1)) == NULL)
+               return createException(MAL, " geomPoints.c:load", " Problem 
allocating space for infoFilename");
+       strcpy(infoFilename, filename);
+       strcpy(infoFilename+strlen(filename), limitsEnding);
+       
+       if ((f = fopen(infoFilename, "rb"))) {
+               //allocate space only if the file exists
+               if ((limits = (pbsm_info*)GDKmalloc(sizeof(pbsm_info))) == 
NULL) {
+                       GDKfree(infoFilename);
+                       infoFilename = NULL;
+                       return createException(MAL, " geomPoints.c:load", " 
Problem allocating space for info");
+               }
+       
+               if (fread(limits, sizeof(*limits), 1, f) != 1) {
+                       fclose(f);
+                       ret = createException(IO, " geomPoints.c:load", " Could 
not read the PBSM index from disk (source: %s)", infoFilename);
+
+                       GDKfree(limits);
+                       limits = NULL;
+                       GDKfree(infoFilename);
+                       infoFilename = NULL;
+                       return ret;
+               }
+
+               fclose(f);
+               GDKfree(infoFilename);
+               infoFilename = NULL;
+       } else {
+               ret = createException(IO, " geomPoints.c:load", " Could not 
open file for reading (source: %s)", infoFilename);
+
+               GDKfree(infoFilename);
+               infoFilename = NULL;
+               return ret;
+       }
+       
+       if((idxFilename = (char*) 
GDKmalloc(strlen(filename)+strlen(idxEnding)+1)) == NULL)
+               return createException(MAL, " geomPoints.c:load", " Problem 
allocating space for idxFilename");
+       strcpy(idxFilename, filename);
+       strcpy(idxFilename+strlen(filename), idxEnding);
+       
        if ((f = fopen(idxFilename, "rb"))) {
+               //allocate space only if file exists
+               if ((pbsm_idx = (pbsm_ptr*)GDKmalloc(USHRT_MAX * 
sizeof(pbsm_ptr))) == NULL) {
+                       GDKfree(limits);
+                       limits = NULL;
+                       GDKfree(idxFilename);
+                       idxFilename = NULL;
+                       return createException(MAL, " geomPoints.c:load", " 
Problem allocating space for pbsm_idx");
+               }
                if (fread(pbsm_idx, sizeof(pbsm_idx[0]), USHRT_MAX, f) != 
USHRT_MAX) {
                        fclose(f);
-                       ret = createException(IO, " geomPoints.c:load", " Could 
not read the PBSM index from disk (source: %s)", idxFilename);
-                       goto clean;
+                       GDKfree(limits);
+                       limits = NULL;
+                       GDKfree(pbsm_idx);
+                       pbsm_idx = NULL;
+                       GDKfree(idxFilename);
+                       idxFilename = NULL;
+                       return createException(IO, " geomPoints.c:load", " 
Could not read the PBSM index from disk (source: %s)", idxFilename);
                }
                fclose(f);
-               
-               if ((f = fopen(limitsFilename, "rb"))) {
-                       if (fread(limits, sizeof(*limits), 1, f) != 1) {
-                               fclose(f);
-                               ret = createException(IO, " geomPoints.c:load", 
" Could not read the PBSM index from disk (source: %s)", limitsFilename);
-                               goto clean;
-                       }
-
-                       fclose(f);
-               } else {
-                       ret = createException(IO, " geomPoints.c:load", " Could 
not read file %s.", limitsFilename);
-                       goto clean;
-               }
-               
-               if ((f = fopen(dataFilename, "rb"))) {
-                       if (fread(oids, sizeof(oids[0]), limits->oidsNum, f) != 
limits->oidsNum) {
-                               fclose(f);
-                               ret = createException(IO, " geomPoints.c:load", 
" Could not read the PBSM index from disk (source: %s)", dataFilename);
-                               goto clean;
-                       }
-
-                       fclose(f);
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to