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: Leo Singer <email@hidden>
- Date: Wed, 18 Feb 2009 03:31:56 -0500
Actually, if the big array and the loop are moved from the C++ method
into the Objective C selector - (void) main then the same
EXEC_BAD_ACCESS occurs. So the problem is not related to C++. The
following source code still exhibits the same problem:
//////////////// TestOperation.h /////////////////////
#import <Cocoa/Cocoa.h>
@interface TestOperation : NSOperation {
}
@end
//////////////// End of TestOperation.h /////////////////////
//////////////// TestOperation.mm /////////////////////
#import "TestOperation.h"
@implementation TestOperation
- (void) main
{
int bigArray[256000];
for (int j = 0 ; j < 256000 && ![self isCancelled] ; j ++)
{
bigArray[j] = 2*j;
}
}
@end
//////////////// End of TestOperation.mm /////////////////////
On Wed, Feb 18, 2009 at 3:16 AM, Leo Singer <email@hidden> wrote:
> Hi,
>
> 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?
>
> Thanks,
> Leo
>
> //////////////// TestOperation.h /////////////////////
> #import <Cocoa/Cocoa.h>
>
> class TestOperationImpl {
> private:
> bool cancelled;
> public:
> TestOperationImpl();
> void go();
> void cancel();
> };
>
> @interface TestOperation : NSOperation {
> TestOperationImpl* testOpImpl;
> }
> - initWithController: (TestOperationController*) controller;
> @end
>
> //////////////// End of TestOperation.h /////////////////////
>
> //////////////// TestOperation.mm /////////////////////
> #import "TestOperation.h"
>
> TestOperationImpl::TestOperationImpl(TestOperationController* controller)
> : cancelled(false)
> {
> }
>
> void TestOperationImpl::go()
> {
> int bigArray[256000];
>
> for (int j = 0 ; j < 256000 && !cancelled ; j ++)
> {
> bigArray[j] = 2*j;
> }
> }
>
> void TestOperationImpl::cancel()
> {
> cancelled = true;
> }
>
> @implementation TestOperation
> - initWithController: (TestOperationController*) ctrl
> {
> if (self = [self init])
> testOpImpl = new TestOperationImpl(ctrl);
> controller = ctrl;
> return self;
> }
> - (void) dealloc
> {
> delete testOpImpl;
> [super dealloc];
> }
> - (void) cancel
> {
> testOpImpl->cancel();
> [super cancel];
> }
> - (void) main
> {
> testOpImpl->go();
> }
> @end
>
> //////////////// End of TestOperation.mm /////////////////////
>
_______________________________________________
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