Stack size of secondary threads, significant differences between DEBUG and RELEASE versions
In my iPhone application (XCode 3.2.4, iOS3.1.3), if I run the application in RELEASE mode, all is fine, but in DEBUG mode, the application crashes with an EXC_BAD_ACCESS exception. The application does some complex calculations. All the main code is contained in several C++ static librar开发者_开发知识库ies and the UIApplication only creates an object from one of these libraries and calls a method of this object.
If I put into a secondary thread the code which calls the complex calculations, I still have the same behavior: EXC_BAD_ACCESS exception in DEBUG mode and no problem in RELEASE mode.
Then I've looked around the thread stack size. By default, iOS set a thread stack size with 512 Kbytes for secondary threads and 1024 Kbytes for the main thread. I've looked the minimal required value for the thread stack size to run correctly my application. I've found the following result: 40 Kbytes for the RELEASE version. 1168 Kbytes for the DEBUG version.
The value 1168 Kbytes in DEBUG version explains why in the main thread, the application will crash (defaut stack size for the main thread is 1024 Kbytes).
I really don't understand why the required thread stack size is so different between the RELEASE and DEBUG version of my application (40 KB vs 1168 Kb !!!). I would like any help to understand this issue.
Thank you. Marc
It is not unusual for debug versions of code and libraries to contain extra self-tests, extra local variables and verification. Perhaps these are increasing the needs of your code.
In particular, it is relatively easy to define some buffer as a local variable and take large amounts of stack. You might find something like this in one or more places taking up stack:
#ifdef _DEBUG
testBuffer[bufferSize];
#endif
If 'bufferSize' was defined as 10K, that would use up 1/4 of your entire 40K stack.
Or perhaps a debug-only function uses a large amount of stack.
It may also be that the settings of your debug build are using any number of Apple's test settings. Things like MallocStack, GuardMalloc, NSZombiesEnabled will require more memory.
精彩评论