ios dealloc with autoreleasepool
-
27-04-2021 - |
Question
I may have the terminology wrong in my question, but here's my best attempt:
the ARC
for my ios app has the autoreleasepool
enabled. Therefore I don't release memory at my choosing, but it seems like it's on a GC
pickup rather.
I'm having trouble getting dealloc
to run for a particular UIView
that has been removed from it's super
view. Is this normal?
Solution
All ARC does is make sure that retain and release messages are sent at the appropriate times (when you start and stop referring to an object). There is no GC -- the deallocs are exactly as before -- which is they are done when the retain count gets to 0.
The autorelease pool is for when you call autorelease instead of release. This means to delay the release call by putting it into a queue managed by an autorelease pool -- when the pool is drained, every object in the queue has release called on it.
So, if you are depending on ARC --
don't call retain, release or autorelease yourself -- you get a build error if you try anyway
use (strong) properties to hold onto references that you need to stay around
if you end up with a circular reference -- a strong pointer pointing to an object that strong points back -- then this all falls apart -- ARC won't release anything. You either need to set one of the pointers to nil, or make one of them weak.
Put break-points where you think the dealloc should be called and check the objects retainCount. Use the Leaks instrument to figure out what might be going on.