Singleton again, but with multi-thread and Objective-C
Posted
by Tonny Xu
on Stack Overflow
See other posts from Stack Overflow
or by Tonny Xu
Published on 2010-04-12T05:56:36Z
Indexed on
2010/04/12
6:03 UTC
Read the original article
Hit count: 522
I know Singleton pattern has been discussed so much. But because I'm not fully understand the memory management mechanism in Objective-C, so when I combined Singleton implementation with multithreading, I got a big error and cost me a whole day to resolve it.
I had a singleton object, let's call it ObjectX
. I declared an object inside ObjectX
that will detach a new thread, let's call that object objectWillSpawnNewThread
, when I called
[[ObjectX sharedInstance].objectWillSpawnNewThread startNewThread];
The new thread could not be executed correctly, and finally I found out that I should not declare the object objectWillSpawnNewThread
inside the singleton class.
Here are my questions:
- How does Objective-C allocate static object in the memory? Where does Objective-C allocate them(Main thread stack or somewhere else)?
- Why would it be failed if we spawn a new thread inside the singleton object?
I had searched the Objective-C language [ObjC.pdf
] and Objective-C memory management
document, maybe I missed something, but I currently I could not find any helpful information.
© Stack Overflow or respective owner