Oscar
Oscar

Reputation: 95

Dealloc called but memory not freed

Im using Storyboards in my app and I have noticed the following. View Controller A is the root VC, pushes View Controller B, who then presents modally VCC. I have an unwind segue from VCC to VCA when the user presses a button. Putting logs on the dealloc method of VCB and VCC y see that they are called when the unwind segue occurs, so that's fine. However none of the memory allocated is freed at that moment. Is this a normal behavior, where the memory is not freed instantly but rather later?

Upvotes: 2

Views: 451

Answers (2)

Sam Falconer
Sam Falconer

Reputation: 488

I see 2 possibilities here:

  1. You aren't calling [super dealloc] all the way up to NSObject, so the memory is never freed. (In non-ARC code, you should always call [super dealloc] at the end of every dealloc you write.)

  2. You are running with NSZombies enabled, so the memory isn't actually freed when dealloc is called.

Upvotes: 1

Oscar
Oscar

Reputation: 95

Found the answer to my own question. Through view controllers B and C I'm using a bunch of images, and I've loaded those with

[UIImage imageNamed:@"imageName"];

which caches all the images I've used for faster reloading them later, therefore not releasing that memory when the view controllers are deallocated.

The other option is to use

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"]];

which doesn't caches the images and all the memory is then released upon deallocation. But I'm going to roll with the first option since I will be using these images frequently through the app.

This post was useful https://stackoverflow.com/a/17569993/5009180

Upvotes: 2

Related Questions