• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Running out of memory on stack in C++ routine invoked within Cocoa NSOperation


  • Subject: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
  • From: Leo Singer <email@hidden>
  • Date: Wed, 18 Feb 2009 03:16:39 -0500

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

  • Follow-Ups:
    • Re: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
      • From: Steve Christensen <email@hidden>
    • Re: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
      • From: Michael Vannorsdel <email@hidden>
    • Re: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
      • From: Leo Singer <email@hidden>
  • Prev by Date: Re: Converting .wav files to MP3
  • Next by Date: Re: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
  • Previous by thread: Re: Converting .wav files to MP3
  • Next by thread: Re: Running out of memory on stack in C++ routine invoked within Cocoa NSOperation
  • Index(es):
    • Date
    • Thread