Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in ShapeKitGeometry.mm #7

Open
H95518 opened this issue Jul 24, 2014 · 2 comments
Open

Memory leak in ShapeKitGeometry.mm #7

H95518 opened this issue Jul 24, 2014 · 2 comments

Comments

@H95518
Copy link

H95518 commented Jul 24, 2014

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;

}

@andreacremaschi
Copy link

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?

@H95518
Copy link
Author

H95518 commented Jul 25, 2014

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
#7 (comment).

Hongwei

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants