views:

130

answers:

1

Using the PhotoScroller example from Apple, to reuse the memory allocated for views, I cannot get the memory released once the retain count hits 0. Here my code for a better understanding: This piece is an extract from PhotoScroller
PhotoViewController.m

- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index  
{  
    page.index = index;  
    page.frame = [self frameForPageAtIndex:index];  
    [page displayPage:index];  
}

ImageScrollView.h

@interface ImageScrollView : UIView  
{

    UIViewController *vc;
    NSUInteger     index;

}  
@property (assign) NSUInteger index;

- (void)displayPage:(int)indice;

@end

ImageScrollView.m

- (void)dealloc  
{    
 [vc release];  
    [super dealloc];  
}

- (void)displayPage:(int)indice  
{  

     //remove previous view  
     [vc.view removeFromSuperview];  
     [vc release];  
     vc = nil;  
     //NSLog(@"vc retain %i", [vc retainCount]);  

     // make a new viewController for the new page   
     Class clss = NSClassFromString([NSString stringWithFormat:@"page_0%i", indice + 1]);   
     vc = [[clss alloc] initWithNibName:[NSString stringWithFormat:@"page_0%i", indice + 1] bundle:nil];  

     [self addSubview:vc.view];  
}

The classes "page_0x" are UIViewControllers. So far have nothing but a UIImageView on the XIB. An example of page_01:
@implementation page_01

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

Memory peaks to 148 MB on device. Gives me a memory warning Level 1, then releases some memory. Goes down to 95 MB. Keeps going up and down.

A: 

Perhaps your page_0x view controllers are getting freed, but something they've created isn't. Some things to check:

  1. Set a breakpoint in the dealloc method of your page_0x view controllers. Is it getting called?

  2. Check all the IBOutlets and other instance variables of your page_0x view controllers. Are they all being released properly in their class's dealloc method?

  3. Run Build & Analyze. Does it turn up anything?

  4. Try running the Leaks Instrument. It can tell you what kind of objects are actually leaking.

  5. (EDIT) Grasping at straws now. You don't have NSZombieEnabled turned on, do you?

  6. (EDIT 2) You say you're throwing a .png on it. What happens if you remove that .png?

  7. If you're running in the simulator, try it on the device. (See this question.)

cduhn
1. dealloc is getting called. 2. I have nothing on my page_0x viewcontrollers. I am just throwing a png on it, so far. 3. Build and Analyze is not giving me anything weird. 4. No leaks.
GianPac
No NSZombieEnabled. But I did notice that when I get a memory warning level 1, some memory gets deallocated. It goes from 148MB to 95MB. Then it increases again until it hits another memory warning. My question now is if I should drain the pool everytime the object gets a retain count of 0.
GianPac
No leaks on the device. Nothing seems to change if I remove the UIImageView with the png. It seems awkward that it waits for a memory warning to actually free the memory
GianPac