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

  • Home
  • SEARCH
  • 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 9110433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:16:05+00:00 2026-06-17T03:16:05+00:00

Preamble; This is NOT a general I have a giant app with a leak

  • 0

Preamble; This is NOT a general “I have a giant app with a leak” question. It’s a specific issue about either Automatic Reference Counting not working properly in a nearly trivial demo app, with full source code, or a subtle code generation or compiler issue, or a bug in Instruments. (TLDR: Oh. Actually an odd little race condition)

I’m confused by the fact that Instruments’ “Allocations” list is showing an instance leak and yet, I have an instance of that class, only one, and ARC is causing the dealloc method to be called and I know it’s being called because there is an NSLog message that gets printed when the dealloc is done, and yet it still shows up in the list of leaks in Instruments.

The retainCount never exceeds 1. It is not being retained by anybody, and it’s being dealloc’d,and yet it appears like it’s a “leak” because it show as an active instance in Instrument’s leaks.

How is that possible?

I am still learning Objective-C with ARC, and so I think I must be making a common beginner mistake. Here is my only init and my object’s dealloc:

- (id) initWithMessage:(NSString*)messageForUser
{
    self = [super init];
    if (self)
    {
        _message = messageForUser;
        NSLog( @"from constructor: %@",_message);
    }
    return self;

}

- (void)dealloc {
    NSLog(@"Goodbye cruel world. One WPMyObject signing off.");
   // [message release]; // ARC forbiddeth thee! Begone release.
    _message = nil;

   // [super dealloc]; // ARC forbiddeth explicit super dealloc
}

Just to see if I could, I tried to call [super dealloc] in the dealloc method, and ARC blocks you with an error, which is great because it’s going to do that for you. However when I write my own init method, it does not block me.

When I run the program using “Run” in XCode, I get the “goodbye cruel world” NSLog message, as I hoped I would, when dealloc is run, yet I also get this evidence that the instance still exists at the end of the run, when Instruments analysis using memory leaks template is used:

If I run without the instance creation code below, I get only a few standard library malloc leaks reported, but if I add this code, all the leaks occur:

  WPMyObject * myObject = [[WPMyObject alloc] initWithMessage: @"Hello World!\n" ];

Here is what I’m seeing, and think I understand is telling me that WPMyObject’s only instance is leaking:

enter image description here

The full source code which is quite trivial (a tiny Mac OS X command line app using Objective-C and Foundation/Foundation.h) is on BitBucket. Click this link to view the source code in your browser.

The main.m unit runs a single test object instance creation, inside an @autoreleasepool {...} context statement:

enter image description here

If you want to see the totally trivial code on your own computer, grab it like this:

  hg clone https://bitbucket.org/wpostma/objectivecplaymac

Update: You can fix the “leak” (which may be a bug in Instruments, or a clang/llvm compiler bug, and not a “real leak”) by adding this line of code just before the } which ends the autorelease pool:

  NSLog( @"Reached end of autorelease pool" );

Yep. Add log message. “Leak” goes away. This is XCode 4.5.2 (4G2008a) on Mac OS X 10.7.5, containing Instruments Version 4.5 (build 4523).

Update2: It really is a simple multi-process race condition. The code below appears to be a reasonable Debug-build bit of hackery. If there was a more explicit way of saying “wait until Instruments is done with me, and then exit main()”, that might be a nice future feature, Apple engineers. A PROFILER_SYNC(“MESSAGE”) macro that expands to nothing in release mode, but in debug builds, sends “MESSAGE” to the profiler…. It could be very handy indeed.

int main(int argc, const char * argv[])
{
    // This creates and cleans up an auto-release pool context.
    @autoreleasepool {

        foo(); // Microscopic amounts of debug code you want profiler to analyze go here.

#ifdef DEBUG
        usleep(10000); // race condition prevention in debug builds, so Instruments can finish up.
#endif

    }
#ifdef DEBUG
    usleep(10000); // race condition prevention in debug builds, so Instruments can finish up.
#endif

   return 0;
}
  • 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-06-17T03:16:06+00:00Added an answer on June 17, 2026 at 3:16 am

    Sounds less like a leak and more like a race condition. A race between the writing of the NSLog and the program’s execution being terminated. Or a buffering issue, more likely, where the output isn’t flushed until a certain threshold is reached.

    Try putting a sleep(100); after the closing brace on the autorelease pool. Or try adding a bunch more text to the log message in the dealloc.

    If that doesn’t “fix” it, then show the disassembly and see what changes between the two versions of the code.


    The reason why this happens: Both Instruments and NSLog() are effectively buffered for performance reasons. They are designed for use in a relatively long running process that almost always has either a main event loop or a call through to dispatch_main().

    This is done to try and minimally impact the performance of the target application, but it can lead to weirdness in micro benchmarks where the process is extremely short lived.

    If you have a minimal test case in a short lived process, I would recommend closing the main() with [[NSRunLoop currentLoop] run];. That’ll run forever and give you a viable runtime for Instruments’ and/or the debugger’s sake.

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

Sidebar

Related Questions

Preamble: This is very close to this question , and I have read this
Preamble: I know, disabling warnings is not a good idea. Anyway, I have a
Preamble I've searched to see if I can find this question anywhere on here
Preamble: I'm Italian, sorry for my bad English. So this is the question: considering
DISCLAIMER: This question is only for those who have access to the econometrics toolbox
PREAMBLE: this question is quite obsolete, it was written when the preferred Android dev
Preamble So, this question has already been answered, but as it was my first
Preamble: My core question is very similar to this one: How can I write
Preamble I'm asking this question because even though I've read through a lot of
Preamble To build dynamic web-sites, we have to master at least four languages: HTML

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.