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 10-25-2010, 02:45 PM   #1 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default App crashes after switching views a number of times?!

I always thought that the OS dealt with memory when it comes to using viewControllers.

At the moment I have 3 views, the first is of course loaded into the Navigation Controller at startup and I have a button which when tapped adds a subview, on the subview is another button and once tapped removes the subview and then allocates a viewController and pops this new view onto the stack. The new view has a back button and when tapped pops the view.

It all works fine until I continually tap from the subview to add the viewController and back again, eventually it crashes but I don't really know why. I am releasing my objects and as I said I thought the OS would deal with it if memory was getting low.

Here's my code when the subview button is tapped to allocate the viewController:
Code:
-(IBAction) twoPlayerButton :(id)sender
{
	newTimer = [NSTimer scheduledTimerWithTimeInterval:0.40  target:self selector:@selector(show2PlayerScreen) userInfo:nil repeats:NO];
	NSString *path = [[NSBundle mainBundle] pathForResource:@"ButtonPress" ofType:@"mp3"];
	AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    self.buttonPress = theAudio;
	[buttonPress play];
	[theAudio release];
	
	white2PlayerGameFlash.animationImages = [NSArray arrayWithObjects:  
											 [UIImage imageNamed:@"White2PlayerGame.png"],
											 [UIImage imageNamed:@"WhiteBG.png"],
											 [UIImage imageNamed:@"White2PlayerGame.png"],
											 [UIImage imageNamed:@"WhiteBG.png"],
											 [UIImage imageNamed:@"White2PlayerGame.png"],
											 nil];
	
	white2PlayerGameFlash.animationDuration = 0.35;
	white2PlayerGameFlash.animationRepeatCount = 1;
	[white2PlayerGameFlash startAnimating];
	
}
And this is the code which fires the timer and adds the viewController:

Code:
-(void) show2PlayerScreen
{

UINavigationController *navController = self.navigationController;

TwoPlayerGame *twoPlayerGame = [[TwoPlayerGame alloc] initWithNibName:@"TwoPlayerGame" bundle:Nil];;

[navController pushViewController:twoPlayerGame animated:NO];
[twoPlayerGame release];
Here's what the console is telling me:
Code:
2010-10-25 20:24:50.537 LightsOut[349:207] Received memory warning. Level=1
2010-10-25 20:25:06.883 LightsOut[349:207] Received memory warning. Level=1
2010-10-25 20:25:19.809 LightsOut[349:207] Received memory warning. Level=1
2010-10-25 20:25:21.534 LightsOut[349:207] Received memory warning. Level=1
2010-10-25 20:25:25.712 LightsOut[349:207] Received memory warning. Level=2
Program received signal:  “0”.
So as soon as it receives memory warning level 2 the app crashes. Can anyone advise me as to how I might resolve this or what I'm doing wrong? Thanks.

Oh I'm running this on the iPad, not sure if that is relevant!
ChrisYates is offline   Reply With Quote
Old 10-25-2010, 04:22 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Where is the subview being removed?
thomashw is offline   Reply With Quote
Old 10-25-2010, 04:43 PM   #3 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default

I'm removing it in viewDidDisappear.
ChrisYates is offline   Reply With Quote
Old 10-25-2010, 05:09 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Can you show more of your code?

Also, what's the reasoning for the timer instead of just calling the method?
thomashw is offline   Reply With Quote
Old 10-25-2010, 05:40 PM   #5 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default

Hey thomashw, the reason I'm going with a timer is I have 5 images which flash up before I wish to call the method.

Not sure which part of the code you're looking for but my .m file is quite long:

Here's the header file:

Code:
@interface OutViewController : UIViewController {
	IBOutlet UIImageView *largeBulb;
	IBOutlet UIImageView *largeBulb2;
	IBOutlet UIImageView *smallBulb;
	IBOutlet UIImageView *smallBulb2;
	IBOutlet UIImageView *whiteFlash;
	IBOutlet UIImageView *whiteFlash2;
	IBOutlet UIView *multiPlayerView;
	IBOutlet UIImageView *white2PlayerGameFlash;
	int myInt;
	
	NSTimer *myTimer;
	NSTimer *newTimer;
	NSTimer *fireTimers;
	
	AVAudioPlayer *staticSounds;
	AVAudioPlayer *scarySounds;
	AVAudioPlayer *buttonPress;	
	
}

@property (retain, nonatomic) AVAudioPlayer *staticSounds;
@property (retain, nonatomic) AVAudioPlayer *scarySounds;
@property (retain, nonatomic) AVAudioPlayer *buttonPress;

-(IBAction) singlePlayerButton :(id)sender;
-(IBAction) multiPlayerButton :(id)sender;
-(IBAction) instructionsButton :(id)sender;

-(IBAction) backButton :(id)sender;

-(IBAction) twoPlayerButton :(id)sender;


@end
Here's the end of the .m file:

Code:
- (void)viewWillAppear:(BOOL)animated
{
		
	[super viewWillAppear:YES];
	myInt = 2;
	NSLog(@"myInt %d", myInt);
	
}

- (void)viewWillDisappear:(BOOL)animated
{
	
	[super viewWillDisappear:YES];
	myInt = 1;
	NSLog(@"myInt %d", myInt);
	
}
- (void)viewDidDisappear:(BOOL)animated
{
	[multiPlayerView removeFromSuperview];
	scarySounds = nil;
	staticSounds = nil;
	buttonPress = nil;
	myTimer = nil;
	newTimer = nil;
	
}

- (void)viewDidUnload {
	
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	
}

- (void)dealloc {
	[buttonPress release];
	[staticSounds release];
	[scarySounds release];
    [super dealloc];
}
ChrisYates is offline   Reply With Quote
Old 10-25-2010, 06:28 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

You should be invalidating/releasing your NSTimer's and releasing your UIImageView's.
thomashw is offline   Reply With Quote
Old 10-25-2010, 07:12 PM   #7 (permalink)
Velvet Rock Apps
 
Join Date: Sep 2010
Posts: 231
ChrisYates is on a distinguished road
Default

Thanks thomashw you made me look harder at my code, I wasn't deallocating an image view in my viewContoller, now I can tap my buttons until my heart is content without one single memory warning
ChrisYates is offline   Reply With Quote
Old 10-25-2010, 07:42 PM   #8 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Glad you figured it out.

Forget about the outlets. I assumed you had them set as properties. I believe I'm still correct about the timers though.
thomashw is offline   Reply With Quote
Old 10-26-2010, 03:14 AM   #9 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,971
Bertrand21 is on a distinguished road
Default

Quote:
Originally Posted by thomashw View Post
Glad you figured it out.

Forget about the outlets. I assumed you had them set as properties. I believe I'm still correct about the timers though.
Pretty sure the timers invalidate themselves when the repeat is set to NO.
__________________
Haters gonna Hate
Likers gonna Like
Bertrand21 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: 331
13 members and 318 guests
7twenty7, chiataytuday, condor304, Creativ, Desert Diva, Domele, dre, dreamdash3, mottdog, palme2elie, Paul Slocum, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,659
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dreamdash3
Powered by vBadvanced CMPS v3.1.0

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