Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-04-2010, 08:29 AM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 8
isoft is on a distinguished road
Default Memory leaks NSMutableArray

Hi,

Im not sure it's me (Properly is..) but I just don't understand the memory leaks I get in my program after upgrading to 4.1

In my .h file I have:
NSMutableArray pieces;

In the Init method I do following:
pieces = [[NSMutableArray alloc] init];

Then I load some pieces to the array..

-(void) loadImages() {

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

//Piece extends UIImageView, just added some new properties to this object
Piece *pieceImageView = [[Piece alloc] initWithImage:Image];
...
...

[pieces pieceImageView];

// Add puzzel piece to the view
[self.view insertSubviewieceImageView atIndex:subViewIndex];
subViewIndex++;

// Clean up
[pieceImageView release];
}


When the user have looked though the pieces and want to go to next view I (Same view new images)
I would like to clean up, then I call same method again (loadImages)


BUT, when I try to clean-up it looks like I get some leaks, and do not understand why.. I have many many different ways non of them works:

- (void) cleanUp() {

1)
NSMutableArray *delete = [NSMutableArray array];

for(Piece *p in pieces) {
[delete addObject];
[p removeFromSuperView];
}

for (Peice *dt in delete)
[pieces removeObject:dt];


2)
for (UIView *view in self.view.subviews) {

if ([view isKindOfClass:[Tile class]])
[view removeFromSuperview];

[pieces removeAllObjects];

3)
for(Piece *p in pieces)
[p removeFromSuperView];

// This crach, when removing from superview I can see I loose ref to it.. So why do I get the leak
[pieces release];
}

I hope one of you can tell me what Im doing wrong, I know it's me, just can't figure it out..

Thank you!

Last edited by isoft; 12-04-2010 at 12:45 PM.
isoft is offline   Reply With Quote
Old 12-04-2010, 11:07 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

What type of object are you leaking, and from which line? Tiles or an array or both?

I'd write the cleanup like this:
Code:
for(Piece *p in pieces){
    [p removeFromSuperView];
}

[pieces removeAllObjects];
You don't want to release pieces (causing it to be destroyed) because you need it around when loadImages is called.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-04-2010, 12:40 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 8
isoft is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
What type of object are you leaking, and from which line? Tiles or an array or both?

I'd write the cleanup like this:
Code:
for(Piece *p in pieces){
    [p removeFromSuperView];
}

[pieces removeAllObjects];
You don't want to release pieces (causing it to be destroyed) because you need it around when loadImages is called.
The type of leak I get when I use Instruments it's the Piece obejcts (It match the amount in my array)

Each time when Im clearing the pieces I get the image leak!

If I use your sample above the for loop works just fine, but calling the [pieces removeAllObjects] crash the program!

I get this error: Program received signal: “EXC_BAD_ACCESS”.

Do not understand this error, this should be very simple but I must be missing something!

Thx for your reply!
isoft is offline   Reply With Quote
Old 12-04-2010, 02:46 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

EXC_BAD_ACCESS is common when you release too much.

Can you post your whole loadimages and cleanup method? Use [code] [ /code] tags to preserve the indentation.

Do you add more objects to "pieces" in any other method?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-04-2010, 03:06 PM   #5 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 8
isoft is on a distinguished road
Default

Im only adding images to my array in loadImages

Code:
-(void) loadImages {
    for (int i = 0; i < imagesToLoad; i++) {

        Piece *pieceImageView = [[Piece alloc] initWithImage:tileImage];

         // Here some calculation regarding the frame!
        pieceImageView.frame = pieceFrame;
        pieceImageView.originalPosition = orgPosition;
        pieceImageView.currentPosition = orgPosition;
			
        // free up some resources 
        pieceImage release];
			
        [pieces addObject:pieceImageView];
			
        // Add puzzel piece to the view
        [self.view insertSubview:pieceImageView atIndex:subViewIndex];
        subViewIndex++;

        // Clean up
        [pieceImageView release];
    }
}
if there is a double touch I get the find the piece tabbed
Code:
-(Piece *) getPieceAtPoint:(CGPoint) point {
	
	CGRect touchRect = CGRectMake(point.x, point.y, 1.0, 1.0);
	
	for( Piece *p in pieces ) {
		
		if( CGRectIntersectsRect(p.frame, touchRect) ) {
			return p; 
		}		
	}
	return nil;
}
I do not retain or relase this object anywhere!

When im done, and want to release my array and reload the view with new images I tried with the code you gave me!

Code:
for (Piece *p in pieces) 
    [p removeFromSuperview];

[pieces removeAllObjects]; // This line crash (EXC_BAD_ACCESS)
Before executing the line removeAllObjects I check the content of the array, there is 4 objects in the array as expected but they are all out of scope, which I think is because of the removeFromSuperview
isoft is offline   Reply With Quote
Old 12-04-2010, 03:13 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

What is this?
Code:
        // free up some resources 
        pieceImage release];
Is pieceImage involved with pieceImageView in any way?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-04-2010, 03:18 PM   #7 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 8
isoft is on a distinguished road
Default

Ohh sorry, type mistake.. Im working on two version right now, and have started renaming, so I wrote wrong:

pieceImage is the image which is loaded with UIImageView

It should have been:
Code:
-(void) loadImages {
    for (int i = 0; i < imagesToLoad; i++) {

        Piece *pieceImageView = [[Piece alloc] initWithImage:pieceImage];

         // Here some calculation regarding the frame!
        pieceImageView.frame = pieceFrame;
        pieceImageView.originalPosition = orgPosition;
        pieceImageView.currentPosition = orgPosition;
			
        // free up some resources 
        pieceImage release];
			
        [pieces addObject:pieceImageView];
			
        // Add puzzel piece to the view
        [self.view insertSubview:pieceImageView atIndex:subViewIndex];
        subViewIndex++;

        // Clean up
        [pieceImageView release];
    }
}
isoft is offline   Reply With Quote
Old 12-04-2010, 03:27 PM   #8 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

You're releasing pieceImage every time you create an ImageView; I'm surprised it doesn't crash right there. Don't release pieceImage inside the loop. You should only release it if you alloc'd it or retained it yourself, and only once.

That's probably why it doesn't crash when you leak the UIImageViews, but does crash when you dispose of them properly.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-04-2010, 03:31 PM   #9 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 8
isoft is on a distinguished road
Default

I do alloc the pieceImage in the for loop..

Sorry I didn't post that, but thought it did not have any meaning..

Code:
-(void) loadImages {
    for (int i = 0; i < imagesToLoad; i++) {

	CGImageRef pieceImageRef = CGImageCreateWithImageInRect(
                                                  orgImage.CGImage, frame );
	
        UIImage *pieceImage = [UIImage imageWithCGImage:pieceImageRef];
        [Piece *pieceImageView = [[Piece alloc] initWithImage:pieceImage];

         // Here some calculation regarding the frame!
        pieceImageView.frame = pieceFrame;
        pieceImageView.originalPosition = orgPosition;
        pieceImageView.currentPosition = orgPosition;
			
        // free up some resources 
        pieceImage release];
        CGImageRelease(pieceImageRef);
			
        [pieces addObject:pieceImageView];
			
        // Add puzzel piece to the view
        [self.view insertSubview:pieceImageView atIndex:subViewIndex];
        subViewIndex++;

        // Clean up
        [pieceImageView release];
    }
}

Last edited by isoft; 12-04-2010 at 03:33 PM.
isoft is offline   Reply With Quote
Old 12-04-2010, 05:19 PM   #10 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

You did not alloc pieceImage in the loop; you created it using imageWithCGImage:, not with alloc/init.

I still think you are over-releasing pieceImage. The cleanup code looks correct to me.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 12-04-2010, 09:26 PM   #11 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,971
Bertrand21 is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
You did not alloc pieceImage in the loop; you created it using imageWithCGImage:, not with alloc/init.

I still think you are over-releasing pieceImage. The cleanup code looks correct to me.
Yes exactly. Your releasing an autorelease object.
__________________
Haters gonna Hate
Likers gonna Like
Bertrand21 is offline   Reply With Quote
Old 12-05-2010, 01:52 AM   #12 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 8
isoft is on a distinguished road
Default

Thx a lot for your help...

Feel a bit stupid now, what you mentioned did the trick!

Have a good one!
isoft is offline   Reply With Quote
Old 12-05-2010, 10:28 AM   #13 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

No worries, sometimes the cause is far from the effect.

When getting an EXC_BAD_ACCESS you can also turn on NSZombieEnabled; that would have told you that you're accessing a UIImage that has been deallocated, which would have been another clue to the real issue.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 367
6 members and 361 guests
chemistry, daudrizek, HemiMG, Kirkout, MarkC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:51 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0