Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView initWithFrame not releasing memory with ARC

I've been using instruments to find allocations that aren't being released properly. I have MKMapViewDelegate that adds a map via a instance method after it has been instantiated. I can see in the call tree that this method keeps retaining about 300KB of memory, after the ViewDelegate is released. I commented out the meat of the code and it stills maintains the memory with just this line:

self.map = [[MKMapView alloc] initWithFrame:CGRectMake(10, 210, 300, 125)];

I look in the object list and the MKMapView itself isn't living, but as I keep creating new ViewDelegates, that memory keeps adding up. Here is how map is defined:

@property (strong, nonatomic)        MKMapView *map;

The map's delegate is set to nil, as well as the reference on the ViewDelegate's dealloc

self.map.delegate = nil;
self.map = nil;
like image 404
spedly Avatar asked Nov 13 '22 20:11

spedly


1 Answers

Once you set the delegate to nil, there are no longer any pointers, and iOS will release it when it wants to. iOS may not release the memory immediately after setting it to nil.

Remember, that you are removing the pointer by setting it to nil, but the object still remains on the heap, basically doing nothing, until something else gets allocated there.

(I'm assuming you also removed the MKMapView from its superview using [self.map removeFromSuperView]).

like image 118
Aaron Brager Avatar answered Nov 25 '22 18:11

Aaron Brager