Buyin Brian
Buyin Brian

Reputation: 2991

Memory Management question

What is the best approach to release multiple instances of an object that you can't release right away?

For example, if you are creating a bunch of thumbnails:

   for(int i=0; i <totalThumbs; i++){        

     Thumb *newThumb = [[Thumb alloc] initWithImage:someImage]

     //position the thumbs here, etc.

     //assume releasing here breaks the app because we need to interact with the thumbs   later
     // [newThumb release] --- breaks the app         

    }

Would it make sense to put all the new objects in an array and release them all in viewDidUnload when we no longer need them?

Upvotes: 0

Views: 82

Answers (4)

Tovi7
Tovi7

Reputation: 2953

Maybe I'm missing something, but why not use autoreleasepools?

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

for(int i=0; i <totalThumbs; i++){        
  Thumb *newThumb = [[[Thumb alloc] initWithImage:someImage]autorelease];
}

[pool drain];

Calling autorelease will add it to the pool (that you can create in any scope you like). Just call drain (or release) on the pool when you're done with it. This will release all queued objects.

Upvotes: 2

Nitish
Nitish

Reputation: 14113

We should always avoid allocating memory in a loop. In your case you should release memory immediately after using the object you have created. i.e

for(int i=0; i <totalThumbs; i++){          

   Thumb *newThumb = [[Thumb alloc] initWithImage:someImage];  

   //position the thumbs here, etc.  

   //assume releasing here breaks the app because we need to interact with the thumbs later  
   // [newThumb release] --- breaks the app  
   // Work with newThumb  
   [newThumb release];
}  

By doing this the objects get released each time loop runs. Actually each time the loop runs, a new object is created. This is how you can manage memory allocation in a loop.

Cheers!

Upvotes: -1

Mark Granoff
Mark Granoff

Reputation: 16938

Presumably you are adding each newThumb as a subview of some other view or to an array, so you should be fine to do that and then release newThumb here. For example:

Thumb *newThumb = [[Thumb alloc] initWithImage:someImage];
[myThumbs addObject:newThumb];
[newThumb release];

This works becuase myThumbs retains the object.

In order not to leak the memory, especially if you regenerate the thumbnails, you would want to iterate over the superview's subviews (all the thumbs), remove each from the superview, and release them. You may also need to do this in you dealloc method where you release the superview (assuming you do that). With an array, you could simply call removeAllObjects, I believe.

Upvotes: 3

nubbel
nubbel

Reputation: 1582

You can release them right after adding to the array, because the array retains them:

for(int i=0; i <totalThumbs; i++){        
    Thumb *newThumb = [[Thumb alloc] initWithImage:someImage]

    //position the thumbs here, etc.

    [thumbsArray addObject:newThumb];

    [newThumb release]; // --- doesn't break the app         

}

In viewDidUnload and/or dealloc release the array. You don't need to release every single thumb.

Upvotes: 1

Related Questions