ShapeKit icon indicating copy to clipboard operation
ShapeKit copied to clipboard

Memory leak in ShapeKitGeometry.mm

Open H95518 opened this issue 11 years ago • 2 comments

The code should be changed to like this:

-(id)initWithWKB:(const unsigned char *) wkb size:(size_t)wkb_size { self = [self init]; if (self) {
GEOSContextHandle_t handle = (GEOSContextHandle_t)self.handle;

    GEOSWKBReader *WKBReader = GEOSWKBReader_create_r(handle);
    _geosGeom = GEOSWKBReader_read_r(handle, WKBReader, wkb, wkb_size);
    GEOSWKBReader_destroy_r(handle, WKBReader);

    char *typeString = GEOSGeomType_r(handle, self.geosGeom);
    self.geomType = [NSString stringWithUTF8String: typeString];
    delete typeString;

    GEOSWKTWriter *WKTWriter = GEOSWKTWriter_create_r(handle);

    char *wktString = GEOSWKTWriter_write_r(handle, WKTWriter, self.geosGeom);
    self.wktGeom = [NSString stringWithUTF8String:wktString];
    delete wktString;

    GEOSWKTWriter_destroy_r(handle, WKTWriter);
}
return self;

}

-(id)initWithWKT:(NSString *) wkt { self = [self init]; if (self) {
GEOSContextHandle_t handle = (GEOSContextHandle_t)_handle; GEOSGeometry *geosGeom = self.geosGeom;

    GEOSWKTReader *WKTReader = GEOSWKTReader_create_r(handle);
    _geosGeom = GEOSWKTReader_read_r(handle, WKTReader, [wkt UTF8String]);
    GEOSWKTReader_destroy_r(handle, WKTReader);

    char *typeString = GEOSGeomType_r(handle, geosGeom);
    self.geomType = [NSString stringWithUTF8String:typeString];
    delete typeString;

    GEOSWKTWriter *WKTWriter = GEOSWKTWriter_create_r(handle);
    char *wktString = GEOSWKTWriter_write_r(handle, WKTWriter, geosGeom);
    self.wktGeom = [NSString stringWithUTF8String:wktString];
    delete wktString;

    GEOSWKTWriter_destroy_r(handle, WKTWriter);
}

return self;

}

-(id)initWithGeosGeometry:(void *)geom { self = [self init]; if (self) { GEOSContextHandle_t handle = (GEOSContextHandle_t)_handle;

    _geosGeom = (GEOSGeometry *)geom;

    char *typeString = GEOSGeomType_r(handle, _geosGeom);
    self.geomType = [NSString stringWithUTF8String:typeString];
    delete typeString;

    GEOSWKTWriter *WKTWriter = GEOSWKTWriter_create_r(handle);
    char *wktString = GEOSWKTWriter_write_r(handle, WKTWriter, _geosGeom);
    self.wktGeom = [NSString stringWithUTF8String:wktString];
    delete wktString;

    GEOSWKTWriter_destroy_r(handle, WKTWriter);
}
return self;    

}

-(id)initWithCoordinate:(CLLocationCoordinate2D)coordinate { self = [self init]; if (self) {

    GEOSContextHandle_t handle = (GEOSContextHandle_t)_handle;

    GEOSCoordSequence *seq = GEOSCoordSeq_create_r(handle, 1,2);
    GEOSCoordSeq_setX_r(handle, seq, 0, coordinate.longitude);
    GEOSCoordSeq_setY_r(handle, seq, 0, coordinate.latitude);
    GEOSGeometry *newGeosGeom = GEOSGeom_createPoint_r(handle, seq);
    NSAssert (newGeosGeom != NULL, @"Error creating ShapeKitPoint");
    _geosGeom=newGeosGeom;

    // TODO: Move the destroy into the dealloc method
    // GEOSCoordSeq_destroy(seq);

    _coords = (CLLocationCoordinate2D *) malloc( sizeof(CLLocationCoordinate2D) );
    *_coords = coordinate;

    GEOSWKTWriter *WKTWriter = GEOSWKTWriter_create_r(handle);
    char *wktString = GEOSWKTWriter_write_r(handle, WKTWriter, newGeosGeom);
    self.wktGeom = [NSString stringWithUTF8String: wktString];
    delete wktString;
    GEOSWKTWriter_destroy_r(handle, WKTWriter);        

}    
return self;

}

H95518 avatar Jul 24 '14 17:07 H95518

hi, thank you for the heads up. As I understand it your changes has to do with wkt strings allocated and never destroyed: ok, it makes sense. More broadly speaking, when you propose a code changes on github you should create a new pull request: https://help.github.com/articles/using-pull-requests Do you feel like trying to create one to resolve this issue?

andreacremaschi avatar Jul 25 '14 07:07 andreacremaschi

Hello, Andrea,

Thank you for the reply. I will try to create a pull request.

Hongwei Shen

On Fri, Jul 25, 2014 at 3:15 AM, Andrea Cremaschi [email protected] wrote:

hi, thank you for the heads up. As I understand it your changes has to do with wkt strings allocated and never destroyed: ok, it makes sense. More broadly speaking, when you propose a code changes on github you should create a new pull request: https://help.github.com/articles/using-pull-requests Do you feel like trying to create one to resolve this issue?

— Reply to this email directly or view it on GitHub https://github.com/mweisman/ShapeKit/issues/7#issuecomment-50115692.

Hongwei

H95518 avatar Jul 25 '14 12:07 H95518