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 08-22-2010, 03:54 AM   #1 (permalink)
Jason Nezumi
 
Join Date: Aug 2010
Location: Mountain Home, ID
Posts: 4
JasonNezumi is on a distinguished road
Unhappy [<Some Object> numberOfSectionsInTableView:]

Ok, this problem has me so completely stumped I'm potentially about to lose my best clients over it. I can't account for the behavior in any way I know how, so I'm gonna see if anybody else can spot the problem here or at least give me a suggestion.

Basically, the application's structure is simple. There is a generic Table View Controller object that derives the menu structure of the application out of NSArray objects supplied by a plist file. The leaves of this tree structure are web views, fed a URL to local files, some URL's, some PNG's. Some menu entries terminate without leading to a file.

Now, here's the problem. We want rotation support for certain end files. I've implemented the standard rotation methods and it's working out pretty fantastically. The problem is that there are crashes every so often with 'Unrecognized Selector' exceptions.


I've gotten this error on a 'UISearchDisplayController' and a 'UITableViewCell'. The table views are working properly whenever they're displayed, but when the web view is displayed, and then rotated, the table views invalidate their rows and begin scanning the delegate and data source for content. Unfortunately, whatever they're inspecting seems to be a more or less random object. Here's the traceback from a recent crash:

Code:
2010-08-22 02:32:35.607 PsychDx[232:207] -[UISearchDisplayController numberOfSectionsInTableView:]: unrecognized selector sent to instance 0x5fdbbc0
2010-08-22 02:32:35.694 PsychDx[232:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISearchDisplayController numberOfSectionsInTableView:]: unrecognized selector sent to instance 0x5fdbbc0'
*** Call stack at first throw:
(
	0   CoreFoundation                      0x024e5919 __exceptionPreprocess + 185
	1   libobjc.A.dylib                     0x026335de objc_exception_throw + 47
	2   CoreFoundation                      0x024e742b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
	3   CoreFoundation                      0x02457116 ___forwarding___ + 966
	4   CoreFoundation                      0x02456cd2 _CF_forwarding_prep_0 + 50
	5   UIKit                               0x0046aba8 -[UITableViewRowData(UITableViewRowDataPrivate) _updateNumSections] + 111
	6   UIKit                               0x0046a934 -[UITableViewRowData invalidateAllSections] + 66
	7   UIKit                               0x00327550 -[UITableView(_UITableViewPrivate) _updateRowData] + 113
	8   UIKit                               0x00320978 -[UITableView numberOfSections] + 39
	9   UIKit                               0x005451ea -[UISearchDisplayController _updateNoSearchResultsMessageVisiblity] + 72
	10  Foundation                          0x00021c1d _nsnote_callback + 145
	11  CoreFoundation                      0x024bdcf9 __CFXNotificationPost_old + 745
	12  CoreFoundation                      0x0243d11a _CFXNotificationPostNotification + 186
	13  Foundation                          0x000177c2 -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
	14  UIKit                               0x002dee30 -[UIWindow _setRotatableClient:toOrientation:duration:force:] + 4393
	15  UIKit                               0x002d87aa -[UIWindow _setRotatableViewOrientation:duration:force:] + 81
	16  UIKit                               0x002db837 -[UIWindow _updateInterfaceOrientationFromDeviceOrientation:] + 164
	17  Foundation                          0x00021c1d _nsnote_callback + 145
	18  CoreFoundation                      0x024bdcf9 __CFXNotificationPost_old + 745
	19  CoreFoundation                      0x0243d11a _CFXNotificationPostNotification + 186
	20  Foundation                          0x000177c2 -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
	21  UIKit                               0x00465965 -[UIDevice setOrientation:animated:] + 228
	22  UIKit                               0x002c4ee0 -[UIApplication handleEvent:withNewEvent:] + 4660
	23  UIKit                               0x002bd074 -[UIApplication sendEvent:] + 71
	24  UIKit                               0x002c1ac4 _UIApplicationHandleEvent + 7495
	25  GraphicsServices                    0x030daafa PurpleEventCallback + 1578
	26  CoreFoundation                      0x024c6dc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
	27  CoreFoundation                      0x02427737 __CFRunLoopDoSource1 + 215
	28  CoreFoundation                      0x024249c3 __CFRunLoopRun + 979
	29  CoreFoundation                      0x02424280 CFRunLoopRunSpecific + 208
	30  CoreFoundation                      0x024241a1 CFRunLoopRunInMode + 97
	31  GraphicsServices                    0x030d92c8 GSEventRunModal + 217
	32  GraphicsServices                    0x030d938d GSEventRun + 115
	33  UIKit                               0x002c5b58 UIApplicationMain + 1160
	34  PsychDx                             0x000022fc main + 102
	35  PsychDx                             0x0000228d start + 53
)
terminate called after throwing an instance of 'NSException'

The clincher here is that the code doesn't crash consistently. I've had it occur the first time I rotated a view, and I've had it occur the second or third time, on a different view. Since rotation is supported exclusively on web views, I honestly have no idea why the table views are getting notifications of the rotation event at all!

And there's one final piece in this puzzle that seems to indicate the issue, at least in some manner or another. While trying to generate a second stack trace to show that the same problem can occur to multiple objects, sometimes, I simply get a good old fashioned 'EXC_BAD_ACCESS', without any stack trace at all even though the debugger's following execution.

Any help at all would be appreciated . . .
JasonNezumi is offline   Reply With Quote
Old 08-22-2010, 08:23 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Sounds like a memory management problem. Look for something that is either over released or insufficiently retained.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-22-2010, 03:12 PM   #3 (permalink)
Jason Nezumi
 
Join Date: Aug 2010
Location: Mountain Home, ID
Posts: 4
JasonNezumi is on a distinguished road
Default lolwut?!

Wow, that's so bizarre. There were two excess retains when I ran the program through analysis, and fixing those up took the crash away.

It's always been my understanding that an extra retain would just amount to the same thing as calling 'alloc()' but never calling 'free()'--that the memory would remain allocated, but it wouldn't interfere with the program.

How is it that an object, allocated but allowed to fall out of memory, could cause this type of bug?
JasonNezumi is offline   Reply With Quote
Old 08-22-2010, 03:18 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You've misunderstood something. Excess retains result in leaks. Leaks don't cause crashes. I can't think of a reason for removing a retain to fix a crash. Quite the opposite.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-22-2010, 03:53 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

Quote:
Originally Posted by JasonNezumi View Post
Wow, that's so bizarre. There were two excess retains when I ran the program through analysis, and fixing those up took the crash away.

It's always been my understanding that an extra retain would just amount to the same thing as calling 'alloc()' but never calling 'free()'--that the memory would remain allocated, but it wouldn't interfere with the program.

How is it that an object, allocated but allowed to fall out of memory, could cause this type of bug?
I'd be very cautious before declaring the problem resolved. Like Brian said, the type of error you saw is indicative of a dangling pointer; the object originally being pointed to was deallocated prematurely, and the memory was reused for some other object, which has no idea how to respond to the message being passed to it. That's why you get random types of objects throwing the Unrecognized Selector exception.

A big problem with these types of bugs is that it's very possible for your app to appear to work normally, even if the problem hasn't been fixed. Remember, when an object is deallocated, it isn't necessarily removed from memory right away; if the system doesn't need to reclaim that memory to use for something else, your deallocated object will stick around and continue to respond to messages passed to it as if everything was normal. Sometimes, the problem never manifests itself until you try running the app on a different platform (e.g., it works in the simulator, but not on the device, or it works on an iPhone but crashes on an iPod touch).

The point is that it's very possible that you haven't actually fixed the bug, but you've just stopped seeing it because an unrelated change to your app coincidentally caused it to stop placing a new object in that specific location in memory. You need to go back and keep track of the retains and releases being sent to the destroyed object and figure out if and when it's being under-retained or over-released.
ChrisL is offline   Reply With Quote
Old 08-22-2010, 04:34 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 16
Stubborn Dreams is on a distinguished road
Default

Code:
2010-08-22 02:32:35.694 PsychDx[232:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISearchDisplayController numberOfSectionsInTableView:]: unrecognized selector sent to instance 0x5fdbbc0'
From the console, po 0x5fdbbc0 will show which object is being called.

Something still seems fishy. Removing extra retains should not have fixed your crash.

Are you releasing any auto-released objects?

What is your view hierarchy? From your description, is it a) a table view that is used to pick which web resource to load into a UIWebView or b) a table view with web views inside the actual table? Do you have a root view controller that manages both the table view and leaf web views?
__________________
My collaborations:
Stubborn Dreams is offline   Reply With Quote
Old 08-24-2010, 03:13 PM   #7 (permalink)
Jason Nezumi
 
Join Date: Aug 2010
Location: Mountain Home, ID
Posts: 4
JasonNezumi is on a distinguished road
Default

Ok, here's the breakdown.

Most of this code isn't mine, I'm doing feature additions. The problem ONLY occurs during rotate, and it hasn't happened anywhere else. Running the analyzer over the application revealed two leaked objects, but no double-releases.

For the reasons mentioned above, I was telling my client straight away that there was no way those leaks were causing the bug. But after fixing them, the bug's symptoms did go away. I'm sort of ambivalent about working really hard to fix this bug, which I know makes me a bad programmer, but I'm only getting paid for feature additions.

I'm not averse to poking around it a bit, and at least warning the client that the bug may have persisted if it doesn't show.

Basically, is the suggestion that I ought to start walking carefully through the release counts to check the situation?

The one thing that confuses me is that I've had autorelease problems, and I've seen the stack trace before. The thing is that an arbitrary object will be subbed for whatever is supposed to be receiving the method call, and it seems the only way that could be explained is if there's an array of objects and calls are occurring via an offset into that stack, and some source object is getting its 'index' wrong.

But that doesn't seem like a reasonable approach for any modern OS, mobile or otherwise, hence my confusion.
JasonNezumi is offline   Reply With Quote
Old 08-24-2010, 03:22 PM   #8 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

Quote:
Originally Posted by JasonNezumi View Post
The one thing that confuses me is that I've had autorelease problems, and I've seen the stack trace before. The thing is that an arbitrary object will be subbed for whatever is supposed to be receiving the method call, and it seems the only way that could be explained is if there's an array of objects and calls are occurring via an offset into that stack, and some source object is getting its 'index' wrong.

But that doesn't seem like a reasonable approach for any modern OS, mobile or otherwise, hence my confusion.
Unless I'm misunderstanding what you're describing, this sounds exactly like the dangling pointer issue I discussed above. Or are you referring to something different?
ChrisL is offline   Reply With Quote
Old 08-24-2010, 04:11 PM   #9 (permalink)
Jason Nezumi
 
Join Date: Aug 2010
Location: Mountain Home, ID
Posts: 4
JasonNezumi is on a distinguished road
Default

Quote:
Originally Posted by ChrisL View Post
Unless I'm misunderstanding what you're describing, this sounds exactly like the dangling pointer issue I discussed above. Or are you referring to something different?
Ok, I guess I'm probably the one that doesn't understand. How in the hell is it possible for an allocated object that has been leaked to cause an object that has not been leaked to confuse its references to other objects that have not been leaked?

What's happening here is some object completely unrelated to any of the crashes is leaked. Then, these other objects that are just doing their thing, perfect reference containment and everything, are getting majorly confused about what's the Table View and what's a Table View Cell and what's a Search Results Display Controller.

In other words, this leaked object is making the entire program, start to finish, confused about which objects are which. I can't see any mechanism that'd allow this to be the case.
JasonNezumi is offline   Reply With Quote
Old 08-24-2010, 05:01 PM   #10 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

Quote:
Originally Posted by JasonNezumi View Post
Ok, I guess I'm probably the one that doesn't understand. How in the hell is it possible for an allocated object that has been leaked to cause an object that has not been leaked to confuse its references to other objects that have not been leaked?

What's happening here is some object completely unrelated to any of the crashes is leaked. Then, these other objects that are just doing their thing, perfect reference containment and everything, are getting majorly confused about what's the Table View and what's a Table View Cell and what's a Search Results Display Controller.

In other words, this leaked object is making the entire program, start to finish, confused about which objects are which. I can't see any mechanism that'd allow this to be the case.
What I'm suggesting is that there were probably two completely unrelated bugs in the original app:
  • The object that you were trying to call the method on was deallocated prematurely, leaving a dangling pointer. As discussed above, the behavior of dangling pointers is basically a crapshoot; they may continue to work normally for a while, if the dead object hasn't actually been overwritten in memory yet.
  • There were other completely unrelated memory leaks.
If this is correct, then you fixed the second bug, but you didn't fix the first. The bugs were unrelated, but because fixing the memory leak might have shifted the locations where the app is writing things into memory, it's possible that the memory for the deallocated object is now not being overwritten for a much longer time. This makes it seem that the app is now functioning normally, when in reality things could blow up at any time, if app happens to reuse the dead object's memory for something else and then sends the message to the dangling pointer again.

This is all hypothetical, of course, but a "shape-shifting" object that seems to switch types when you try to call a method is a pretty strong symptom of a dangling pointer. As you said above, it's probably worth walking through and checking retain/release counts of the affected object.
ChrisL is offline   Reply With Quote
Old 08-24-2010, 05:09 PM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by JasonNezumi View Post
Ok, I guess I'm probably the one that doesn't understand. How in the hell is it possible for an allocated object that has been leaked to cause an object that has not been leaked to confuse its references to other objects that have not been leaked?
No one is saying that.
Quote:
What's happening here is some object completely unrelated to any of the crashes is leaked. Then, these other objects that are just doing their thing, perfect reference containment and everything, are getting majorly confused about what's the Table View and what's a Table View Cell and what's a Search Results Display Controller.
No, that is not what is happening. Like Brian said a few posts ago, leaks do not cause crashes. I don't think you know what a leak is, or you would understand this. What you want to look for is a pointer (i.e. a instance variable or property) that got released and then used without an intervening reassignment or allocation of a new object. The pointer is still pointing to memory that may have been reassigned to some other object, because that memory was marked as "available" after it was released.
RLScott is offline   Reply With Quote
Old 08-24-2010, 05:15 PM   #12 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Just to further clarify, you aren't necessarily looking for a stray "release" in code. You could be, but those are generally easy to find. There's one, should that be released, yes/no, next one.

I will guess that what you are looking for is something autoreleased, and this is what I meant by "insufficiently retained". It will likely be something in either an assign property or something that used direct instance variable assignment. It should have been retained, but wasn't, and dies before you try to use it again.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-24-2010, 05:21 PM   #13 (permalink)
Registered Member
 
ziocleto's Avatar
 
Join Date: Nov 2009
Location: London, UK
Posts: 982
ziocleto is on a distinguished road
Default

Generally when I have odd behavious with UITableViews is when I accidentally requests a double whammy reload on the table view itself that will cause 2 or more reloadData notifications to be fired recursively.

IE: calling reloadData on a UITableViewDelegate function that will itself refresh the table.

I have no idea what's your code flow is like but I'll have a look at this nasty case.

Cheers,
Dado.
ziocleto is offline   Reply With Quote
Reply

Bookmarks

Tags
crash, exception, memory management

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: 339
5 members and 334 guests
freewind, HemiMG, lendo, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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