Storing CLLocationCoordinates2D in NSMutableArray

Posted by Amarsh on Stack Overflow See other posts from Stack Overflow or by Amarsh
Published on 2010-04-05T07:54:40Z Indexed on 2010/04/05 8:03 UTC
Read the original article Hit count: 252

Filed under:
|

After some searching, I got the following solution : ref: http://stackoverflow.com/questions/1392909/nsmutablearray-addobject-with-mallocd-struct

CLLocationCoordinate2D* new_coordinate = malloc(sizeof(CLLocationCoordinate2D));
new_coordinate->latitude = latitude;
new_coordinate->longitude = longitude;
[points addObject:[NSData dataWithBytes:(void *)new_coordinate
length:sizeof(CLLocationCoordinate2D)]];
free(new_coordinate);

And access it as:

CLLocationCoordinate2D* c = (CLLocationCoordinate2D*) [[points objectAtIndex:0] bytes];

However, someone claims that there is a memory leak here? Can anyone suggest me where is the leak and how to fix it. Further, is there a better way of storing a list of CLLocationCoordinate2D in NSMutableArray? Please give sample code since I am an Objective C newbie.

© Stack Overflow or respective owner

Related posts about nsmutablearray

Related posts about nsdata