Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:35:12+00:00 2026-05-27T11:35:12+00:00

I have a C++ library that I want to expose as an Objective-C framework,

  • 0

I have a C++ library that I want to expose as an Objective-C framework, so it will be easier to use for Objective-C developers. In wrapping up the C++ library I have come across one particular problem in dealing with autorelease objects and threading.

One feature of the library is that the developer can register a “logger” for receiving notification-messages as callbacks from the library. The notification from the library use C++ types and is received from another (POSIX) thread, so I’ve made a private C++ wrapper class to handle this: it receives the callback, turn the char* argument into an NSString, and pass it on to an Objective-C logger instance provided by the user. This all works very well and looks something like this:

// Is called from the C++ library from another posix thread
void ObjCLoggerWrapper::LogMessage(const char *message)
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  // Pass string to the user-provided Objective-C instance called "Logger"
  [Logger logMessage:[[[NSString alloc] initWithUTF8String:message] autorelease]];
  [pool release];
}

As an example of a user callback I wrote this simple method to collect all logging in a NSString member m_text in the user-class’ instance (to be used elsewhere, but that’s irrelevant).

-(void) logMessage: (NSString*)message
{
  @synchronized(self)
  {
    m_text = [m_text stringByAppendingFormat:@"%04d: %@\r\n", m_lineno++, message];
  }
}

So far so good. Or so I thought. But here’s the beef:

All autoreleased objects in the user’s callback method will belong to the wrapper’s NSAutoreleasePool and hence be released when the callback is completed.

Oops! This means that my m_text string, created implicitly as an autoreleased object by the stringByAppendingFormat message, will be released when logMessage is done and become a zombie. When accessed the next time, the code will crash. The user, of course, certainly and rightfully does not expect this. I myself had to scratch my head a few times before realizing what was going on.

So my question is: How should we deal with autoreleased objects when doing callbacks to user-code from another thread?

I see several possible options. None are perfect and google doesn’t help (hence this question).

  1. Tell the user “don’t create autorelease objects in your callback code”. Not good: such objects are often created involuntarily, eg by stringByAppendingFormat and tons of other framework methods. There’s no warning other than a later, hard-to-debug crash.
  2. Don’t have an NSAutoreleasePool. The lack of one will cause warnings if the user tries to create autoreleased objects. Definitely not pretty, but will alert the user to the problem in a robust manner. And the user could “just” add an NSAutoreleasePool of his own to fix the situation. But again: not pretty.
  3. No NSAutoreleasePool and run the callback on the main thread using performSelectorOnMainThread. Any new autorelease objects would wind up on the main thread’s pool. I think this is safe but would welcome comments – can callbacks always be performed on the main thread, for instance? This approach requires more delicate coding in the wrapper to avoid thread-deadlocks and to wait for the result, but it’s my preferred choice so far.

Just to make it clear: Rewriting my own wrapper is no problem. My main priority is creating a solution that will work smooth and seamlessly for a user of the Objective-C framework. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:35:13+00:00Added an answer on May 27, 2026 at 11:35 am

    This isn’t a problem with your autorelease approach. Your approach looks sound.
    The problem is logMessage as written is fundamentally flawed. It’s going to fail any time there’s a drain of an enclosing autorelease pool between invokations. The main run loop (modulo optimizations) drains its autorelease pool on every event loop spin, so this would fail there just as badly in that case.

    A couple of FWIWs:

    [Logger logMessage:[[[NSString alloc] initWithUTF8String:message] autorelease]];
    

    can be written

    [Logger logMessage:[NSString stringWithUTF8String:message]];
    

    and [pool drain] is preferred over [pool release]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I have a library that use across different websites/ applications I've always just
Let's say you have some 3rd-party library class that you want to extend, simply
I have a date picker library written for MooTools that I want to port
Recently I found a C library that I want to use in my C++
I have a C++ DLL (no code) that I want to expose to .NET
I have a class library, which I want to expose to the outside world
I have a library that I would like to license and distribute. I know
I have a library that interacts with our phone system, ie, Hey phone, call
I have a library that does I/O. There are a couple of external knobs
I have a library that I created with some business logic that includes writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.