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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-28-2009, 09:17 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 4
tittom is on a distinguished road
Default setNeedsDisplay and memory management

Hello, I'm quite new in Iphone development. I have an app which needs to redraw quite often.
Everything is working fine expect that every time I call setNeedsDisplay, for an unknown reason it allocates memory ( I found out that using the object alloc tool) so after a couple of minutes my app crashes because of too much allocated memory.
it seems weird to me cause games need to be updated quite often so there is probably a way around this problem.
Thank you for your time
tittom is offline   Reply With Quote
Old 09-28-2009, 10:45 AM   #2 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 486
CommanderData is on a distinguished road
Default

You're probably going to have to post code for us to see what you're doing wrong. It sounds like you must be creating some objects during the course of updating your screen with drawRect, and not destroying them when done?
__________________
ChronoSoft - Rogue Touch Available now in the App Store! ... Version 1.5 available now!
CommanderData is offline   Reply With Quote
Old 09-28-2009, 10:49 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 4
tittom is on a distinguished road
Default

ok I don't think I'm allocated anything but I'm probably wrong here is the code in my drawfunction :

Code:
- (void)drawRect:(CGRect)rect{
	
	switch (GameState) {
		case eMenu:
			[BGGameImage drawAtPoint:(CGPointMake(0.0,0.0))];
			break;
		case ePlayerChooseCard:
			[BGGameImage drawAtPoint:(CGPointMake(0.0,0.0))];
			[NameOfPlayer drawTextInRect:NameOfPlayer.frame];
			
			// create the circle of card ( change the position of the cards )
			int i = 0;
			int indexCard = 0;
			
			//player 1 pick a card
			if(Player1Playing == true)
			{
				
				//determine the position of each card
				while( i < 51)
				{
					float Angle = (i*2*3.14 + CircleMovement)/NbOfElements;
					Player1[indexCard].XPosition = cos(Angle) * Radius + 100.0;
					Player1[indexCard].YPosition = sin(Angle) * Radius + 1320.0;
					
					indexCard ++;
					i++;
				}
			}
			//player 2 pick a card
			else
			{
				//draw the top image
				//[Player2Image drawAtPoint:(CGPointMake(0.0,0.0))];
				
				while( i < 51)
				{
					float Angle = (i*2*3.14 + CircleMovement)/NbOfElements;
					Player2[indexCard].XPosition = cos(Angle) * Radius + 100.0;
					Player2[indexCard].YPosition = sin(Angle) * Radius + 1320.0;
					
					indexCard ++;
					i++;
				}
			}
			
			//now Draw the cards
			i=0;
			indexCard= 0;
			
			if(Player1Playing == true)
			{
				while(i<51)
				{
					if(Player1[indexCard].Flag == true){
						if(Player1[indexCard].AlreadyPicked == false)
						{
							[Player1[indexCard].Back drawAtPoint:(CGPointMake(Player1[indexCard].XPosition,Player1[indexCard].YPosition))];
						}
						else
						{
							[Player1[indexCard].SmallFront drawAtPoint:(CGPointMake(Player1[indexCard].XPosition,Player1[indexCard].YPosition))];
						}
					}
					i++;
					indexCard++;
				}
			}
			else
			{
				while(i<51)
				{
					if(Player2[indexCard].Flag == true){
						if(Player2[indexCard].AlreadyPicked == false)
						{
							[Player2[indexCard].Back drawAtPoint:(CGPointMake(Player2[indexCard].XPosition,Player2[indexCard].YPosition))];
						}
						else
						{
							[Player2[indexCard].SmallFront drawAtPoint:(CGPointMake(Player2[indexCard].XPosition,Player2[indexCard].YPosition))];
						}
					}
					
					i++;
					indexCard++;
				}
			}
			break;
		case ePlayerReadCard:
			[BGGameImage drawAtPoint:(CGPointMake(0.0,0.0))];
			if(Player1Playing == true)
			{
				[Player1[CardToMove].Front drawAtPoint:(CGPointMake(0.0,0.0))];
			}
			else
			{
				[Player2[CardToMove].Front drawAtPoint:(CGPointMake(0.0,0.0))];
			}
			First = true;
			Anim = false;
			break;
		default:
			break;
	}
}

Last edited by tittom; 09-29-2009 at 03:21 AM.
tittom is offline   Reply With Quote
Old 09-30-2009, 03:24 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 4
tittom is on a distinguished road
Default

no suggestions ? anyone please
tittom is offline   Reply With Quote
Old 09-30-2009, 10:39 AM   #5 (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

Quote:
Originally Posted by tittom View Post
Everything is working fine expect that every time I call setNeedsDisplay, for an unknown reason it allocates memory ( I found out that using the object alloc tool) so after a couple of minutes my app crashes because of too much allocated memory.
Sort the ObjectAlloc instrument by "net #" and try to see what type of object is climbing up the list. That's the object that is getting created but not released. You can "drill down" into that object in the instrument, and if you use the details pane you can find what line of code creates those objects.

I don't see any object creation in the snippet you posted.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-30-2009, 12:03 PM   #6 (permalink)
Registered Member
 
mobileben's Avatar
 
Join Date: Jul 2009
Location: Zgrunturos
Posts: 161
mobileben is on a distinguished road
Default

As smasher cites, it does not really look like you are doing any object creations in the drawRect routine. However, this does not bar from the SDK itself creating objects.

I'll ask the obvious question. Are you actually sure that the drawRect routine is the problem routine? And if so, how have you validated it?

There are also a few other things you could do to check as well which would be to change your value of 51 to a lower number temporarily like 10 or less and see what effect that has.

I am not an Obj-C guy, all our stuff is in C++ ('cept where necessary) ... but I'll also throw out the question of isn't CGPointMake dynamically creating memory? That could be your problem spot!?
mobileben is offline   Reply With Quote
Old 09-30-2009, 04:10 PM   #7 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 4
tittom is on a distinguished road
Default

Hey guys thanks for your help
It seems that it's Quartzcore whick allocs memory all the time.
And it is probably related I have a memory leak when I do that :
Code:
	Nameview = [[[NameView alloc] initWithNibName:@"NameView" bundle:nil]retain];
	[self addSubview:Nameview.view];
	Nameview.view.hidden = YES;
Do I need to release anything?
Thanks

Last edited by tittom; 09-30-2009 at 04:51 PM.
tittom is offline   Reply With Quote
Old 09-30-2009, 11:56 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

Quote:
Originally Posted by tittom View Post
Hey guys thanks for your help
It seems that it's Quartzcore whick allocs memory all the time.
And it is probably related I have a memory leak when I do that :
Code:
    Nameview = [[[NameView alloc] initWithNibName:@"NameView" bundle:nil]retain];
    [self addSubview:Nameview.view];
    Nameview.view.hidden = YES;
Do I need to release anything?
Thanks

Yes, you should release Nameview, and make sure that it eventually gets removed from the superview when you're done with it.

Don't worry about CGPointMake - CGPoint is a struct and gets allocated on the stack, not the heap. You're not using malloc or alloc, so there's nothing to free, release, or dealloc there.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
memory management, memory problem, setneedsdisplay

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: 397
7 members and 390 guests
13dario13, ChrisYates, fredidf, iOS.Lover, Leslie80, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

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