4

I'm adding functionality to an existing Cocoa application written mostly in Objective-C. I have to use an existing c++ class in the class I'm writing, so the new class is Objective-C++. Furthermore, I've had to write a method on the c++ class that temporarily uses a buffer. So my c++ method is something like

(void) myMethod{
   int8_t* bffr = new int8_t[length];
   // use the buffer
   delete [] bffr;
}

I instantiate the c++ class in my .mm file, and try to carry on. The problem is that the application crashes. However, if I comment out the delete, the app does not crash, but Instruments reports a leak associated with this method. I presume that the Objective-C memory management is getting bolluxed up. How can I resolve this catch-22?

BTW, I get the same result using malloc and get_temporary_buffer.

2
  • 5
    It's quite possible there is an issue with your 'use the buffer' code, please show us what it does. Commented Dec 9, 2011 at 15:04
  • 1
    +1 @JoshuaWeinberg if your buffer is affected to a non-copied ObjectiveC object, it can't be safely deleted. Commented Dec 9, 2011 at 15:06

2 Answers 2

1

You must free the memory you allocated and do so at the right time - only after the time that no other code will ever attempt to access that piece of memory.

Sign up to request clarification or add additional context in comments.

1 Comment

Argh. I've looked at that use the buffer code for a couple of hours and thanks to this reply I notice that I've overwritten bffr. Fixed. Thanks.
0

Have you tried enabling the memory debugging features in Xcode to track down the source of the crash?

(Edit Scheme > Debug > Memory Mgmt, tick all boxes)

Cheers, Jay

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.