Cocoa thread cannot access Cocoa API
I am starting 开发者_开发知识库a new thread from the
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
method, using
MyThread *thread1 = [[MyThread alloc] init] ;
[thread1 start] ;
where MyThread
is a subclass of NSThread
.
If I run an empty for
loop in the thread's main
method and quit, it works fine. But as soon as I try to use a Cocoa API such as NSString
or even NSAutoReleasePool
, my program just hangs by entering the debugger.
What could be the source of the problem ?
[Hint]: I tried stepping thru the debugger and it once gave me a SIGBUS
error. What memory access issues could there be ?
The problem was tricky. I was declaring a large char array of 1 MB in the middle of my main() method. The compiler was pushing this declaration to the top of the compiled method. Somehow this space was too large to ask for and hence the program would crash immediately upon entering the main method. Unfortunately, even when I inserted a return statement much before this declaration, the program would crash because the compiler would push this declaration to the very beginning. I changed that array declaration to a malloc() and the program ran.
精彩评论