Moshe
Moshe

Reputation: 58087

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:

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?

Upvotes: 6

Views: 380

Answers (1)

sergio
sergio

Reputation: 69027

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.

Upvotes: 2

Related Questions