user393964
user393964

Reputation:

Objective-C to C++ string conversion causing memory leak

I'm making a call to a controller string datamember and using the UTF8String method to convert the NSString object into a C++ string object and this is causing me a memory leak. The call is made inside the core audio RenderTone function and looks like this:

InstrumentGridViewController *viewController = (InstrumentGridViewController *)inRefCon;
string cppTrackName = [viewController->trackName UTF8String];

The code is located in a for loop though and it's causing quite a memory leak. Any ideas why? I have to make the conversion because I'm doing a c++ function call with the cppTrackName variable. I also tried this: string cppTrackName = "synthTrack #1"; and this works fine without causing any leaks but obviously I really need to read the value from my controller.

This is the error in console (constantly repeated):

objc[6352]: Object 0x68c3b60 of class __NSCFData autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

Upvotes: 2

Views: 643

Answers (1)

justin
justin

Reputation: 104698

UPDATE

Now that I have seen the message:

The implementation of -[NSString UTF8String] is free to assume an autorelease pool exists on the thread where the message is made.

Your audio callback is being called from a secondary (render) thread. Evidently, no autorelease pool exists on this thread at the time you message the string, thus the "No autorelease pool" console message.

Under typical circumstances, you would create an autorelease pool explicitly. However, this is the render thread. If it is a realtime audio thread, you should not be locking (incl. heap allocations) or even using objc messaging (because it can lock, and objc messaging makes no guarantee about the maximum time a message send takes). ObjC APIs have (virtually) no place in realtime audio render contexts.

Upvotes: 2

Related Questions