Search code examples
iosobjective-cautomatic-ref-countingdealloc

In IOS, ARC, although the reference Count drops to 0, the object still hadn't been freed


My Code is like below, under ARC in IOS7.1.

void function
{
  for (int i = 0; i < 100000; i++)
    {
        UIImage *buttonBar_test = [[UIImage alloc] init];
        //[buttonBar_test release];
    }

}

Although the ref count drops to 0, the temp objects(buttonBar_test) still alive, not freed by the system after the function returns. Why?

![in the instrument->allocations, enable to "Record reference counts"][1]

![after the function returns, the temp objects still got alive, very strange?!][2]

![the reference count drops to 0, but no "free" was called on the objects, so the objects are still alive][3]

I created a demo project, this issue doesn't exist. But in my own project, it exist. I don't know why, are there any configuration related to this issue?

(Also, my own project links some 3rd party dependencies which didn't enable arc, but in my global linker flag settings, it contains: -fobjc-arc)


Solution

  • If dealloc was called, but the memory backing the object was not released, there are two possibilities:

    1. You did not call [super dealloc] at the end of your -dealloc (only applies to non-ARC code)
    2. You have Zombies enabled. This can be done either via a checkbox in Instruments (in the configuration for the ObjectAlloc instrument), or via the Diagnostics tab in the Scheme editor.

    Most possible that you have Zombies enabled,
    Here is a small guide how to disable it.

    1) Open scheme editor.

    enter image description here

    2) Click on Run $<Project name> (tag 1)
    3) Click on Diagnostics (tag 2)
    4) Uncheck Enable Zombie Objects.

    enter image description here