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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 02-09-2010, 12:08 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 33
Default Getting rid of Memory Warnings

Hi,
I am just about to finish my first iphone game, it is actually a boxing game inn which an object and a player try to knock out each other. I have so many different animation for each punch and its reactions on getting punch. Now problem is that after two or three round games started giving memory warnings and after few minutes application get terminated .
I used both "imageName" and "imageWithContent" for caching/non-caching but it makes very slightly difference. My all views are deallocating nicely, but still having that memory issue.
I have wasted more than 3 weeks and still have no clue.Please give me some solution how do I get rid of these warning, and run the game smoothly without any interference.
asimrs is offline   Reply With Quote
Old 02-09-2010, 02:21 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 31
Default

Quote:
Originally Posted by asimrs View Post
Hi,
I am just about to finish my first iphone game, it is actually a boxing game inn which an object and a player try to knock out each other. I have so many different animation for each punch and its reactions on getting punch. Now problem is that after two or three round games started giving memory warnings and after few minutes application get terminated .
I used both "imageName" and "imageWithContent" for caching/non-caching but it makes very slightly difference. My all views are deallocating nicely, but still having that memory issue.
I have wasted more than 3 weeks and still have no clue.Please give me some solution how do I get rid of these warning, and run the game smoothly without any interference.

This is application specific problem ... however i would first get rid off the autorelease objects and use manual release, since they stay in your memory quite long. If you want to stick with the autorelease, consider using your own autorelease pool for segments of code where you instantiate a lot of objects. You can also use these two sites as they provide excellent advice in memory management.

10 iPhone Memory Management Tips | akosma software

iPhone Dev Center: Memory Management Programming Guide for Cocoa: Introduction
mistergod is offline   Reply With Quote
Old 02-09-2010, 04:00 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 33
Default

Quote:
Originally Posted by mistergod View Post
This is application specific problem ... however i would first get rid off the autorelease objects and use manual release, since they stay in your memory quite long. If you want to stick with the autorelease, consider using your own autorelease pool for segments of code where you instantiate a lot of objects. You can also use these two sites as they provide excellent advice in memory management.

10 iPhone Memory Management Tips | akosma software

iPhone Dev Center: Memory Management Programming Guide for Cocoa: Introduction
Thanks mistergod,

I am rarely using auto-release, objects are being released as per requirements. Actually when I test it on simulator or on device memory gets increases and it never fall back on switching or releasing the views. I am using too many keyframe animations with high graphic images, so here I have some doubts, would you like if I share code for you?

One more thing I need to ask, how much it makes difference if I test it on iphone 3G, currently I am testing it on iphone 2G???
asimrs is offline   Reply With Quote
Old 02-09-2010, 05:39 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

If instruments says your net memory is going up continuously then you aren't releasing something properly. If you use the Object Allocations instrument and set an "inspection range" you should be able to tell what kind of objects are being created. You can also look at the net number of UIImage or UIImageView objects and see if that's what's increasing over time.

If it looks like those objects are the problem, then post an example of how you're adding an image to the screen.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-10-2010, 10:10 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 33
Default

Quote:
Originally Posted by smasher View Post
If instruments says your net memory is going up continuously then you aren't releasing something properly. If you use the Object Allocations instrument and set an "inspection range" you should be able to tell what kind of objects are being created. You can also look at the net number of UIImage or UIImageView objects and see if that's what's increasing over time.

If it looks like those objects are the problem, then post an example of how you're adding an image to the screen.
Here are two methods for loading images, creating animations

-(NSArray *)loadImpactImagesNSString *)file_prefix NSInteger)loop_start NSInteger)loop_end
{

//set an array for computer impact images
NSMutableArray *imageArray =[NSMutableArray array];// [[NSMutableArray alloc] init];
//imageArray=nil;

NSString *cname;
NSString *imagePath;
int i;
UIImage *img;
for (i = loop_start; i <=loop_end ; i++) {



if([file_prefix isEqualToString:@"eclat vitre_00"])
{
//this is only for glass break aniamtion
cname = [NSString stringWithFormat:@"%@%d.png",file_prefix, i];

}
else
{
cname = [NSString stringWithFormat:@"%@%@%03d",levelPrefix,file_pref ix, i];
imagePath = [[NSBundle mainBundle] pathForResource:cname ofType:@"png"];
}

//UIImage *img = [UIImage imageNamed:cname];


img = [UIImage imageWithContentsOfFile:imagePath];

if (img)
{
[imageArray addObject:img];


}else{ NSLog(@"%@ not found",cname);}

img=nil;

pauseState=file_prefix;
pauseStateFrameNo = loop_end;

//}else{ NSLog(@"%@ not found",cname);}

}


//z [imagesCopy arrayByAddingObject:imageArray];
return imageArray;
}




//general function for animating the object moves
-(void)myAnimateObject{

//if(movingObject!=nil && sizeof(listdata)>0)
//{

movingObject.animationImages = listdata;


if(speed != customAnimationSpeed)
{
movingObject.animationDuration = customAnimationSpeed;
}
else
{
movingObject.animationDuration = speed ;//speed * ([listdata count]);
}


//movingObject.animationDuration = 0.5;// speed * ([listdata count]);

movingObject.contentMode = UIViewContentModeBottomLeft;


if(repeatAnimation==0)
{
[movingObject setAnimationRepeatCount:1];

}


[self.view addSubview:movingObject];

//NSLog(@"Added subview %@", movingObject);

[movingObject startAnimating];

self.pauseImage=[listdata objectAtIndex[listdata count]-1)];



movingObject.image=pauseImage; // logic to put last image of animation just after finishing the animation
[self.view bringSubviewToFront:glassCrackObject];



}
asimrs is offline   Reply With Quote
Old 02-10-2010, 11:49 AM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

You did not say what kind of object is increasing over time.

Is "movingObject" created in code, or in Interface Builder? If it's in code, how do you create it and how do you remove it when finished?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-11-2010, 01:07 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 33
Default

Quote:
Originally Posted by smasher View Post
You did not say what kind of object is increasing over time.

Is "movingObject" created in code, or in Interface Builder? If it's in code, how do you create it and how do you remove it when finished?
Thanks Smasher,

Here is the function in which which is resposible for loading and animating the object, I have created all object in code.

movingObject is created as follows:
movingObject = [[UIImageView alloc] initWithFrame: CGRectMake(00, 60, 400, 280)];


//load computer punch move
-(void)loadPunchint)bodyFace int)leftRight
{

//Settings *gameSettings = [[Settings alloc] init];

NSString *strike;

int pStart,pEnd;

computerBlockFaceBody=bodyFace;

if(computerBlock==1)//block
{


if(bodyFace==1)//block towards body
{


if(leftRight==1)
{

//pStart=1; pEnd=9;
pStart=1; pEnd=6;
strike = [NSString stringWithFormat:@"block_degrad_body_left"];

}
else
{
//pStart=1; pEnd=9;
pStart=1; pEnd=6;
strike = [NSString stringWithFormat:@"block_degrad_body_right"];
}
}
else //punch towards face
{


if(leftRight==1)
{
//pStart=1; pEnd=8;
pStart=1; pEnd=6;
strike = [NSString stringWithFormat:@"block_degrad_face_left"];
}
else
{
//pStart=1; pEnd=8;
pStart=1; pEnd=6;
strike = [NSString stringWithFormat:@"block_degrad_face_right"];
}
}

}
else //unblock
{

int pType=[self generateRandomNumBetween:1:4];

switch (pType) {
case 1:
punchType=@"direct";
break;

case 2:
punchType=@"hook";
break;

case 3:
punchType=@"uppercut";
break;
default:
break;
}


if(bodyFace==1)//punch towards body
{


if(leftRight==1)
{

pStart=1; pEnd=6;
strike = [NSString stringWithFormat:@"strike_%@_degrad_body_left",pun chType];
}
else
{
pStart=1; pEnd=6;
strike = [NSString stringWithFormat:@"strike_%@_degrad_body_right",pu nchType];
}
}
else //punch towards face
{


if(leftRight==1)
{
pStart=1; pEnd=6;
strike = [NSString stringWithFormat:@"strike_%@_degrad_face_left",pun chType];
}
else
{
pStart=1; pEnd=6;
if([punchType isEqualToString:@"uppercut"]) {pStart=1; pEnd=6;}
strike = [NSString stringWithFormat:@"strike_%@_degrad_face_right",pu nchType];
}
}

}



self.settingsArray =[gameSettings getCustomSettings:strike];



[movingObject removeFromSuperview];
[movingObject release];
movingObject = [[UIImageView alloc] initWithFrame: CGRectMake(00, 60, 400, 280) ];

self.listdata= [self loadImpactImages :[settingsArray objectAtIndex:degradX/3] Start End];


[self myAnimateObject];

settingsArray=nil;



}
asimrs is offline   Reply With Quote
Old 02-11-2010, 02:32 AM   #8 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Did you use the Object Allocations instrument to find out what kinds of objects are increasing over time?

It's possible that you're not releasing self.listdata in the dealloc, which means the images inside would never be released. Also, they way you're adding and removing movingObject is unusual. That's typically done by init-ing the view, adding it as a subview, and then immediately releasing it. If you do that then you don't have to do the release when you remove movingObject later, and you don't have to release movingObject in the dealloc either.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-11-2010, 02:56 AM   #9 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 33
Default

Quote:
Originally Posted by smasher View Post
Did you use the Object Allocations instrument to find out what kinds of objects are increasing over time?

It's possible that you're not releasing self.listdata in the dealloc, which means the images inside would never be released. Also, they way you're adding and removing movingObject is unusual. That's typically done by init-ing the view, adding it as a subview, and then immediately releasing it. If you do that then you don't have to do the release when you remove movingObject later, and you don't have to release movingObject in the dealloc either.
Smasher, I thinks movingObject is deallocating fine, would you like If I send you complete project code??? Also could you please give me some sample code or hint how do I use movingObject by int-inting the view.
asimrs is offline   Reply With Quote
Old 02-11-2010, 12:51 PM   #10 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by asimrs View Post
Smasher, I thinks movingObject is deallocating fine, would you like If I send you complete project code??? Also could you please give me some sample code or hint how do I use movingObject by int-inting the view.
You can post a link to the project here, but only if you've already checked it with the Object Allocations instrument to find out what object is increasing over time. What object is giving you the trouble?

The typical way to add a subview is this:

Code:
movingObject = [[UIImageView alloc] initWithFrame: CGRectMake(00, 60, 400, 280)];
[self.view addSubview:movingObject];
[movingObject release];
Now "view" is the only thing that's retaining movingObject, so movingObject will get dealloc'ed by the system when the view is destroyed, or whenever movingObject is removed from the view.
__________________

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: 264
17 members and 247 guests
@sandris, ADY, Alsahir, dacapo, Dani77, Desert Diva, djohnson, HemiMG, jansan, JasonR, MarkC, mer10, prchn4christ, ryandb2, smethorst, tomtom100
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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