Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C style blocks cause memory leaks?

I'm working on a kiosk style slideshow app. I have a UIScrollView which shows the slides, and a factory class, which generates the slides. The "slides" themselves are UIViewController subclasses, which are loaded out from XIB files and customized by the factory class. In my main view controller, I set up the scroll view and start a timer. The timer calls a "reload" method every N seconds, which handles the reload and call to the factory class.

The method that the factory class uses looks something like this:

- (SlideViewController *)slideFromManagedObject:(Slide *)managedObject{

  NSInteger slideType = [managedObject slideType];

  switch(slideType){
     case kSlideTypeA:

  { 
       //
       //  configure arguments here
       //

       return [[SlideViewController alloc] initWithArgument:argument] autorelease];
         break;

  }

    //
    //  More types here...
    //

    default:
      break;
  }


}

I haven't yet gotten to the point of defining all of my cases, but the ones that are filled out seem to cause jumps in memory usage. If I add return [[[UIViewController alloc] init] autorelease]; right before the switch/case, I get no visible view, as expected, but I also don't see those memory increases. I'm not sure, but I suspect that it's the "C blocks" that I'm wrapping my slide generation code in.

Some things to note:

  • When the app starts, I see the memory plateau from about 400 kilobytes to around double that. Then, when the slides progress, any of the slides whose generation code is contained in curly braces is called, the memory plateaus upwards again.

  • This behavior only seems to happen once per launch - when the app loops through all of the slides, the plateaus to_not_ happen again. However if the app is backgrounded and then relaunched, the plateaus do occur again, consuming even more memory.

  • When I left the app to run overnight, for about 10 hours and forty minutes, the memory usage had slowly climbed from about 1.44 megabytes to somewhere closer to 1.57 megabytes. I suspect that there are/were some other leaks in there that may have been fixed by my tweaking, but the main jump from about 800 kilobytes to somewhere between 1.4 and 1.5 megabytes is still an issue.

Instruments does not report any leaks, but the plateauing concerns me.

What could be causing the increased memory?

EDIT:

So I don't think it's the blocks, since using an if/else seems to do the same thing. Here's a screenshot of the Allocations instrument running:

enter image description here

Where could possibly be holding on to these views?

like image 760
Moshe Avatar asked Aug 01 '11 18:08

Moshe


Video Answer


1 Answers

One possible explanation for what you are seeing is some caching that UIKit (I assume) is doing of your objects (don't know what they are, but I think of images mostly).

Caching is often used during transitions and for other internalities of UIKit.

UIKit empties its caches usually when a memory warning is received, so you could try and send one to see what happens. In actuality, I suspect that results of sending a memory warning will not be very easy to analyze, since all of your views are also unloaded, hence memory will go down forcibly. But you can try...

As to how sending a memory warning to the device (as opposed to the simulator), here you find an useful S.O. post.

like image 81
sergio Avatar answered Oct 20 '22 05:10

sergio