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 11-19-2011, 07:53 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 135
thephotographer is on a distinguished road
Default app crashing, calls to functions after dealloc

Hi,

im having some issues with NSNotificationCenter and my app crashing. i have custom views which have NSNotificationCenter observers, but i am having issues with my app crashing when a notification is given around the time of the view being removed from the superview and its dealloc being called. in my dealloc i am removing the observer, and cancelling any previous perform requests on each particular method called by any notification observers as follows:

Code:
NSLog(@"dealloc called");
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateStarted" object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(updateStarted:) object: nil];
When my app crashes, in my console i can see my nslog dealloc called. and my console then says gdb, and shows a highlighted green line of code where the crash happened, it is as follows:

Code:
- (void)updateStarted:(NSNotification *)notification
{
    self.updaterLabel.hidden = NO; ---- Thread 1: Program received signal: "EXC_BAD_ACCESS".
    self.activityIndicator.hidden = NO;
    [self.activityIndicator startAnimating];
}

i am having this happen a lot in various classes throughout my app, all to do with notifications being called around the same time as the class's dealloc as the view is removed from the superview. how can i go about fixing this?

thanks!
thephotographer is offline   Reply With Quote
Old 11-19-2011, 09:29 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Post the code where you add the observer. You may want to call removeObserver first thing when viewDidUnload is called.
baja_yu is offline   Reply With Quote
Old 11-19-2011, 09:36 AM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
You may want to call removeObserver first thing when viewDidUnload is called.
Agreed. Also, unless you really want to only remove specific notifications, don't remove them by name. Remove them all instead:

Code:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Yes, in your case this may be the same thing. But with a couple important differences:

1. you don't have to worry about mistyping your notification name (I'd use a constant here, BTW, if you must remove a specific notification)
2. you don't have to worry about modifying your cleanup work when you add or remove notifications
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 11-19-2011, 05:00 PM   #4 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 135
thephotographer is on a distinguished road
Default

Thanks so much guys! dljeffery, i didnt actually know you could do that thats going to save a lot of time! thanks!

to add my observers im writing this in the initWithFrameCGRect)frame

Code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateStarted:) name:@"updateStarted" object:nil];
baja_yu, Thats a great idea! i never even thought of it.. that gives me some ideas, but ill get to that in a minute. Is the viewDidUnload only for view controllers, or can i use it with a normal UIView? The majority of my subviews i add, which most of them have observers, are UIViews. thats just because thats how i have known/worked out how to do it, if theres a better way i dont know about id love to use it! and im sure there is, i am rather new to the whole programming thing, never studied it or read books or anything, just got in and started looking at examples on google.

now the other thing with what you mentioned Baja_yu, related to viewDidUnload, is viewDidAppear, and viewDidDisappear. from what i can tell they notify the view controller that it has come on screen or off screen. that could replace a lot of my notifications. i am basically building a magazine platform, like the adobe publishing suite, can handle a number of different magazine issues, downloaded from a server, all the same features as adobe but a lot more features. im about 90% complete, nearly there! a lot of my notifications are just broadcasting a message to anyone listening, saying page changed. for the elements on the page that need to do something when a page is changed, they add an observer and listen for a page change, such as a video autoplaying, image gallery resetting back to first image when changing to a different page off screen, animations starting to play when the page comes on screen, etc etc. theres 100 different examples, which is why i have to many observers around the place. id love to do it a different way.

originally i did it a different way before i thought of using observers, the original method was fool proof, never crashed once on me, but the observers method is so much easier on writing the code. but im considering going back to the first method for the reliability.

what i was doing is when a page turns, where i currently post a notification saying page turned, i would do a search through all subviews of the magazine, and if the subview was of a certain class, depending on if it was the current viewing page or not, it would directly get sent a message eg [videoController play]; or [imageGallery change to image:0];

i dont know what thats like performance wise, i should put a timer before and after the search to see how long it takes, but i think i will go back to that anyways unless i find something else, as the reliability was 100%, probably because i was directly calling methods in the classes then and there, in the main thread, so it wasnt waiting until later when the main thread is free, by then the class may have been dealloced like whats happening now with the notifications.
thephotographer 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: 386
18 members and 368 guests
7twenty7, Alex-alex, Apptronics RBC, baja_yu, chiataytuday, dedeys78, dre, Duncan C, e2applets, ipodphone, jeroenkeij, leostc, matador1978, mbadegree, n00b, QuantumDoja, Retouchable, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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