I have two View Controllers, one with a grid of buttons, and the second one is a detailed description based on the button the user presses.
I’m using “Mark Heap” in Instruments allocations tool and finding that my app is increasing in memory after the user sees the detailed View Controller and then presses the back button in the navigation bar. IT should result in a net change of zero for memory… I’m using ARC, xcode 4.2.1, deploying to ios 5.0+
When I load the new ViewController after the button press, I load some data images in a background thread. Is it possible that because I’m pressing the back button quickly, that data is still being loaded in the background thread and is never released from memory?
from the slides from the class i had that described memory cycles problem you may be facing:
what if you had the following property of a class?
and then tried to do the following in one of that class’s methods?
all objects referenced inside a block will stay in the heap as long as the block does. (in other words, blocks keep a strong pointer to all objects referenced inside of them.)
in this case, self is an object referenced in this block. thus the block will have a strong pointer to self. but notice that self also has a strong pointer to the block (through its myBlocks proeprty)
THIS IS A SERIOUS PROBLEM!
neither self nor the block can ever escape the heap, now. that’s because there will always be a strong pointer to both of them (each other’s pointer). this is called a memory “cycle”.
solution:
local variables are always strong. that’s okay, because when they go out of scope, they disappear, so the strong pointer goes away. but there’s a way to declare that a local variable is weak. here’s how …
this solves the problem because now the block only has a weak pointer to self. (self still has a strong pointer to block, but that’s okay) as long as someone in the univese has a strong pointer to this self, the block’s pointer is good. and since the block will not exist if self does not exist (since myBlocks won’t exist), all is well!