views:

73

answers:

2

I am trying to figure out how to pass a UIImage around between various view controllers without duplicating the UIImage and increasing memory unnecessarily. The image is passed successfully using the method below, but I cannot seem to free up this UIImage later on, which ends up weighing down my app with 7MB+ of data that cannot be accounted for. What I want to know is, what is happening to memory when I pass a UIImage (belonging to my current view controller) to a view controller I will be switching to next, and then release the original pointer? Like this:

-(IBAction)startEditing:(id)sender
{

    EditViewController *controller = [[EditViewController alloc] initWithNibName:@"EditViewController" bundle:nil];
    controller.delegate = self;

      //imageDataPhoto1 is of type UIImage (from camera)
      controller.editPhotoData1 = imageDataPhoto1;
      [imageDataPhoto1 release];
      imageDataPhoto1 = nil;

        controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:controller animated:YES];

    [controller release];

}

is the DATA of the pointer imageDataPhoto1 not getting released due to it being assigned to the view controller instance before it is released?

+2  A: 

If your EditViewController's .h file declares the editPhotoData1 @property with retain, then it will automatically retain any object assigned to it. You'll need to release the object explicitly, at the very least ,in EditViewController's dealloc method.

As a matter of course, you should make sure to release any properties set to retain in your dealloc method, for all classes, i.e.:

- (void)dealloc
{
    [editPhotoData1 release];
    ...
    [super dealloc];
}
Shaggy Frog
+1  A: 

Read up on UIImage. It does a lot of caching and reloading behind-the-scenes, especially if it gets its pictures from a named file or path. I don't think that duplicating an instance actually creates a second copy. See the description of +imageNamed and others to see what I am talking about:

imageNamed:

Returns the image object associated with the specified filename.

  • (UIImage *)imageNamed:(NSString *)name

Parameters

name The name of the file, including its filename extension. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.

Return Value

The image object for the specified file, or nil if the method could not find the specified image.

Discussion

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

mahboudz