Hi developers,
I am working on an application which uses UIScrollViews to display 8 UIViews with some labels and images (I'm not using the imageNamed method...). Each UIScrollView is inside an UIViewController.
The user can drill down in the data, so the old UIViewController will be released and a new will be created.
Unfortunately there is somewhere a memory leak, which is not displayed in Instruments.
Inside the controller I try to release the scrollView in the dealloc method, but the scrollView, and all underlying views with the images and labels are not being released and the memory consumption grows.
Here is a part the outer Controller which handles the drilldown:
Code:
- (void)drillDown:(int)timePeriod {
// NSLog(@"releasing old statisticsViewController");
if ( statisticsViewController != nil && [[statisticsViewController view] superview] != nil ) {
[[statisticsViewController view] removeFromSuperview];
}
[statisticsViewController release];
NSLog(@"allocation new viewcontroller");
statisticsViewController = [[StatisticsViewController alloc] initWithNibName:nil bundle:nil withTimePeriod: timePeriod];
if ( statisticsViewController != nil && [[statisticsViewController view] superview] != nil ) {
NSLog(@"still connected to superview!!");
}
[view addSubview: [statisticsViewController view]];
}
and this the code from the statistic controller for the creating and filling of the uiscrollview
Code:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 358)];
[scrollView setPagingEnabled: YES];
[scrollView setContentSize: CGSizeMake(320.0f * numberOfPages, 358.0f)];
[scrollView setShowsHorizontalScrollIndicator: YES];
[scrollView setShowsVerticalScrollIndicator: NO];
[scrollView setScrollsToTop: NO];
[scrollView setDelegate: self];
// add the views to the scroll view
for ( int i=0; i < numberOfPages; i ++ ) {
NSString* currentDate = (NSString*)[statisticDates objectAtIndex:i];
StatisticsView* statisticsView = [[StatisticsView alloc] initWithFrame:CGRectMake(0, 0, 320, 358) withDate:currentDate withTimePeriod:timePeriod];
[statisticsView autorelease];
if (nil == statisticsView.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * i;
frame.origin.y = 0;
statisticsView.frame = frame;
}
[scrollView addSubview:statisticsView];
}
I think the issue is because of the allocation and the autorelease of those "StatisticsView"s?
I spend the last couple of day in rebuilding and rearranging the code but I don't get rid of the growing memory usage and the allocated objects. I read the memory guide from apple and googled a lot, but without any success
Any hint would be great!
Thanks for your help
Tom