2

I have two pages, each with a tableView in them. The first tableview leads to the second tableView when a cell is pressed via a push segue. I used "embed in" to apply a navigation controller to the first view controller which gives the second view a back button which works for 99% of the application. For some reason, if I scroll all the way down to the bottom of the second tableview and then press the back button, the application crashes with this error: Thread 1: EXC_BAD_ACCESS. The backtrace from the console is this:

(lldb) bt
* thread #1: tid = 0x9517c, 0x000000011115a00b libobjc.A.dylib`objc_msgSend + 11, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x000000011115a00b libobjc.A.dylib`objc_msgSend + 11
    frame #1: 0x000000010ff1f462 UIKit`-[UIScrollView(UIScrollViewInternal) _notifyDidScroll] + 56
    frame #2: 0x000000010ff0fa42 UIKit`-[UIScrollView setContentOffset:] + 645
    frame #3: 0x000000010ff79b5f UIKit`-[UITableView setContentOffset:] + 362
    frame #4: 0x000000010ff24360 UIKit`-[UIScrollView(UIScrollViewInternal) _adjustContentOffsetIfNecessary] + 1445
    frame #5: 0x000000010ff214ef UIKit`-[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:tramplingDragFlags:] + 417
    frame #6: 0x000000010ff102d6 UIKit`-[UIScrollView removeFromSuperview] + 32
    frame #7: 0x000000010fee9e39 UIKit`-[UIView dealloc] + 404
    frame #8: 0x00000001111588cd libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 591
    frame #9: 0x000000010f045da6 CoreFoundation`_CFAutoreleasePoolPop + 22
    frame #10: 0x000000010f079ef3 CoreFoundation`__CFRunLoopRun + 2051
    frame #11: 0x000000010f079486 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #12: 0x0000000113e329f0 GraphicsServices`GSEventRunModal + 161
    frame #13: 0x000000010fe8b420 UIKit`UIApplicationMain + 1282
  * frame #14: 0x000000010eb9122e appname`top_level_code + 78 at AppDelegate.swift:13
    frame #15: 0x000000010eb9126a appname`main + 42 at AppDelegate.swift:0
    frame #16: 0x000000011191d145 libdyld.dylib`start + 1
(lldb) 

If I scroll slightly up from the bottom of the second tableView, the program does not crash. The way I added the navigation controller was by clicking on the first view controller and using the "embed in" command. When the crash occurs, it takes me to the AppDelegate file as seen here: enter image description here

What could be causing this program to crash in such a specific manner?

A_toaster
  • 1,196
  • 3
  • 22
  • 50

1 Answers1

6

The error messages tells us that UIScrollView (which is part of the TableView) is trying to send a message to its delegate, that delegate appears to be deallocated at that point in time.

It seems like the UITableView is not being deallocated when you hit the back button, but the UIViewController is.

A very similar question has been asked here: UIScrollView EXC_BAD_ACCESS crash in iOS SDK

A first step to track down this problem would be implementing the dealloc method of your UIViewController subclass and setting the delegate of the UITableView to nil.

You should also turn on Zombies (Tracking down zombies with Xcode 5 on ios6) to get more information on which deallocated instance is receiving this message.

Community
  • 1
  • 1
Ben-G
  • 4,996
  • 27
  • 33
  • 2
    Thanks for the response! I ended up setting `self.tableView.delegate = nil` in `viewWillDisappear` and now it doesn't crash! – A_toaster Jan 21 '15 at 01:54
  • 1
    OH MY GOD AFTER HOURS I FINALLY FOUND THIS. Thanks so much!! But I added this code `if self.isMovingFromParentViewController() { ... }` so it would only set it to nil when it is popping (i'm using a `UITabBarController`) – Henry Ngan Jun 24 '15 at 18:04