This is my first post and you are my last hope.
I have a list with images in my iPad app, if you select one, my class MyViewController
which extends UIViewController
is loaded and shown (by just setting window.rootViewController
, by using UINavigationController
, by presentModalViewController
- tried everything, doesn't make any difference). It has a UIScrollView
inside, which adds a big version of the image to the UIScrollView
with this code:
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[[delegate getImageNames] objectAtIndex:(gameNr*2)]]];
tempImageView.frame = CGRectMake(0,0,t开发者_JAVA技巧empImageView.frame.size.width/2,tempImageView.frame.size.height/2);
[self setMyImage:tempImageView];
[tempImageView release];
myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
myScrollView.maximumZoomScale = 2.0;
myScrollView.minimumZoomScale = 1.0;
myScrollView.clipsToBounds = YES;
myScrollView.bounces = NO;
myScrollView.delegate = self;
[myScrollView addSubview: myImage];
myImage
is a (nonatomic, retain)
property of the type UIImageView
inside my MyViewController
.
After pressing a button and go back to the list of images, I release the reference to MyViewController
and it's dealloc method is called like this:
- (void)dealloc
{
CFShow(@"dealloc!");
for(UIImageView *subview in [myScrollView subviews]) {
if(subview.frame.size.width > 20){
[subview removeFromSuperview];
[subview setImage:nil];
}
}
[myImage setImage:nil];
self.myScrollView = nil;
self.myImage = nil;
[myScrollView release];
[myImage release];
[super dealloc];
}
It works so far, but the problem is, that the memory of the images is not really deallocated this way. After opening and closing about 12 images, the app crashes with memory warning. This is also confirmed by the report_memory method I got from here: iOS Low Memory Crash, but very low memory usage
The dealloc method IS called, that's triple checked.
I checked the variables myImage
and myScrollView
via breakpoint in the debugger, they are set to 0x0 by those commands in the dealloc
method. Why is the memory not freed????????
Thanks for any suggestion, I am working on this since three whole days, it's driving me crazy.
Replace this:
self.myScrollView = nil;
self.myImage = nil;
[myScrollView release];
[myImage release];
with this:
[myScrollView release];
self.myScrollView = nil;
self.myImage = nil;
And post code where you define property myScrollView
and init
it.
And you don't need loop for(UIImageView *subview in [myScrollView subviews])
. When you will release
myScrollView
object it will automatically release all its subviews.
精彩评论