4

Call to setContentSize crashes the application after some of the UIScrollView's contents have been removed.

int toolbarHeight = [[[self navigationController] toolbar] frame].size.height;
int navbarHeight = [[[self navigationController] navigationBar] frame].size.height;

int totalHeight = toolbarHeight + navbarHeight;
// contentWidth is 640
CGSize contentSize = CGSizeMake(contentWidth, [scrollView frame].size.height - totalHeight);

[scrollView setContentSize:contentSize]; // Crash happens here, contentSize is perfectly valid

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

What could be causing this?

EDIT: Here's the stack trace for those who can make any sense of it:

* thread #1: tid = 0x1c03, 0x34d2e32c libsystem_kernel.dylib`__pthread_kill + 8, stop reason    = signal SIGABRT
frame #0: 0x34d2e32c libsystem_kernel.dylib`__pthread_kill + 8
frame #1: 0x36c2a20e libsystem_c.dylib`pthread_kill + 54
frame #2: 0x36c2329e libsystem_c.dylib`abort + 94
frame #3: 0x308eff6a libc++abi.dylib`abort_message + 46
frame #4: 0x308ed34c libc++abi.dylib`_ZL17default_terminatev + 24
frame #5: 0x36361356 libobjc.A.dylib`_objc_terminate + 146
frame #6: 0x308ed3c4 libc++abi.dylib`_ZL19safe_handler_callerPFvvE + 76
frame #7: 0x308ed450 libc++abi.dylib`std::terminate() + 20
frame #8: 0x308ee824 libc++abi.dylib`__cxa_rethrow + 88
frame #9: 0x363612a8 libobjc.A.dylib`objc_exception_rethrow + 12
frame #10: 0x34e9050c CoreFoundation`CFRunLoopRunSpecific + 404
frame #11: 0x34e9036c CoreFoundation`CFRunLoopRunInMode + 104
frame #12: 0x3618c438 GraphicsServices`GSEventRunModal + 136
frame #13: 0x31f86e7c UIKit`UIApplicationMain + 1080
frame #14: 0x0000cb50 App`main + 152 at main.m:16
Gerstmann
  • 5,368
  • 6
  • 37
  • 57
  • Please explain your downvotes! Thank you. – Gerstmann Aug 24 '12 at 08:15
  • I think the solution to youir problem is inside your question... Check the callstack on crash and who trying to access that array and why it trying to access the object at unavailable with index. – Tutankhamen Aug 24 '12 at 08:17
  • 1
    @Tutankhamen I don't know about you, but for me, the call stack doesn't say anything – Gerstmann Aug 24 '12 at 08:23
  • you should show us your code that sets the content size.. – Martin Ullrich Aug 24 '12 at 08:23
  • the call stack say, you want to access an object (with index 1) in your empty mutablearray – CarlJ Aug 24 '12 at 08:24
  • you should create a CGSize variable and later call setContentSize so you can set a breakpoint in between and inspect your value – Martin Ullrich Aug 24 '12 at 08:24
  • 1
    The array in question is internal to the UIScrollView(I think it is), so I suppose I don't have any control over it. – Gerstmann Aug 24 '12 at 08:35
  • It looks like exception was thrown somwher in other thread. I recommend you to try to catch NSRangeException exception. Also you have to check your scrollview delegate implementation methods... – Tutankhamen Aug 24 '12 at 08:41
  • You are accessing an element past the end of array somewhere in your code. I doubt it's because you're changing `contentSize`, although the crash may be a side effect of that. Try running with zombies enabled. – nielsbot Aug 24 '12 at 08:58

1 Answers1

5

It seems that the exception is thrown when the content offset is left outside the content size.

I had views side by side in the scroll view and the offset was to the last view.

+------+------+------+
|      |      |offset|
+------+------+------+

Now the last view is removed and the content size changed to a smaller one.

+------+------+
|      |      |offset
+------+------+

The offset is left behind.

I changed the code so that the offset is moved first, then the content size changed.

+------+------+
|      |offset|
+------+------+

No crashes so far.

Gerstmann
  • 5,368
  • 6
  • 37
  • 57