Re: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
Re: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
- Subject: Re: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
- From: Steve Christensen <email@hidden>
- Date: Wed, 18 Feb 2009 07:24:47 -0800
Google is your friend. The first search result for "Mac OS X stack
size" was <http://developer.apple.com/qa/qa2005/qa1419.html>, which
says:
"Each Mac OS X process is launched with a default stack size of
8 Megabytes. This allocation is used exclusively for the main
thread's stack needs. Each subsequent thread created is allocated
its own default stack, the size of which differs depending on the
threading API used. For example, the Mac OS X implementation of
Pthreads defines a default stack size of 512 Kilobytes, while
Carbon MPTasks are created with a 4 Kilobyte stack."
That explains why your array of chars or shorts doesn't cause a crash
since the allocation will be under 512K.
The document also mentions ways to adjust the thread stack size for
pthreads and NSThreads, but it might just be better to allocate your
array on the heap using malloc or new so you don't run into these
sorts of problems. Then you have a way of testing whether or not the
allocation succeeded, versus the crash method you're currently using. :)
steve
On Feb 18, 2009, at 12:16 AM, Leo Singer wrote:
I have a C++ method that I am invoking from within the - (void) main
selector of an NSOperation. My Cocoa application is crashing because
this particular C++ method puts a huge amount of data on the stack. I
am getting an EXEC_BAD_ACCESS error. However, the same C++ routine
works fine if I call it from within a command line C++ program.
I have contrived some sample code to illustrate the problem.
TestOperation is an (Objective C) subclass of NSOperation; I am
running the NSOperation in a separate thread by putting it into an
NSOperationQueue. TestOperationImpl is a C++ class. The NSOperation
is responsible for doing one thing only: calling the go() method on an
instance of TestOperationImpl.
Note the very large array of ints that is declared inside
TestOperationImpl::go(). If it is changed to an array of shorts or an
array of chars, then this example code works fine, no EXEC_BAD_ACCESS.
Is there any way for me to give my application more memory, or at
least give more memory to the thread that is running this C++ method?
//////////////// TestOperation.h /////////////////////
[snip]
void TestOperationImpl::go()
{
int bigArray[256000];
for (int j = 0 ; j < 256000 && !cancelled ; j ++)
{
bigArray[j] = 2*j;
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden