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 6334545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:40:48+00:00 2026-05-24T18:40:48+00:00

I’m having completely unexpected timing problems with the super simple code below. One of

  • 0

I’m having completely unexpected timing problems with the super simple code below. One of the variables is being autoreleased, and I have no clue why. I’m not using autorelease, KVO, etc. It shouldn’t be happening.

The WindowController is set as an @property (retain)‘d of MainController.

In the -dealloc of the MainController, I do self.windowController = nil;

But, it keeps waiting until the autorelease pool is flushed to release the windowController. I expect the WindowController’s dealloc to be called as soon as self.windowController = nil is done. Even if I wrap the [mainController release] in NSAutoreleasePool, it still doesn’t release right away.

Why is this happening?


This doesn’t seem like proper behavior for the @property / NSWindowController. Am I missing something?


Correction: It’s not bindings. I officially have no clue what the problem is.

Main driver:

[[MainController new] release];

MainController.h:

#import <Foundation/Foundation.h>
#import "WindowControllerSubclass.h"
@interface MainController : NSObject {
    WindowControllerSubclass *wc;
}

@property (retain) WindowControllerSubclass *wc;

@end

MainController.m:

#import "MainController.h"

@implementation MainController

@synthesize wc;

- (id)init {
    if (self = [super init]) {
        // This is problem here >>>  If I assign directly to wc, then it's not added to autorelease pool
        self.wc = [[WindowControllerSubclass alloc] init];
        [self.wc release]; // since it's (retain)'d
    }

    return self;
}

- (void) dealloc {
    self.wc = nil;
    NSLog(@"%@ deallocd (should be called after WC's dealloc)", [self className]);
}

@end

MainWindowControllerSubclass.h:

#import <Cocoa/Cocoa.h>

@interface WindowControllerSubclass : NSObject /* Not even NSWindowController */
@end

MainWindowControllerSubclass.m:

#import "WindowControllerSubclass.h"

@implementation WindowControllerSubclass

- (void) dealloc {
    NSLog(@"%@ deallocd", [self className]);
}

@end
  • 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-24T18:40:51+00:00Added an answer on May 24, 2026 at 6:40 pm

    There’s nothing strange about it, especially if your NSWindowController is in an autorelease pool.

    An object (say x) is dealloc’ed when every object which owns it releases it. Autorelease is a deferred release, i.e. it doesn’t actually release until the autorelease pool is drained.

    Consider the following chain of events:

        B creates x
        A owns x
        A autoreleases x. // x is not released; it's put on an autorelease pool
        B releases x.     // x is not dealloced yet, because x is not released by the autorelease pool
        autorelease pool is drained. x is sent another release message. nobody owns x. x is dealloc'd. 
    

    That’s what you’re seeing.

    — Update —

    More, precisely, the mysterious usage of autorelease pool arises from your line

    [self.wc release]; 
    

    This uses the getter of wc, i.e. it calls [self wc]. Now, the default synthesized getter is implemented in this portion of obj-c runtime, in particular objc_getProperty_non_gc. Note that your property is (retain), i.e. it’s (atmomic retain). To guarantee atomicity, the getter retains the ivar and then returns it after autorelease‘ing it:

    id *slot = (id*) ((char*)self + offset);
    if (!atomic) return *slot;
    
    // Atomic retain release world
    spin_lock_t *slotlock = &PropertyLocks[GOODHASH(slot)];
    _spin_lock(slotlock);
    id value = objc_retain(*slot);
    _spin_unlock(slotlock);
    
    // for performance, we (safely) issue the autorelease OUTSIDE of the spinlock.
    return objc_autoreleaseReturnValue(value);
    

    That’s why it’s put on the autorelease pool. In any case,

    obj.property=[[SomeClass alloc] init];
    [obj.property release];
    

    is a bad idea. In your case, self.ivar in the second line returned what you assigned in the first line, but that’s not guaranteed in the case of clever, non-synthesized accessors, or in a multi-threaded environment. When you do

    obj.property=x;
    id y=obj.property;
    

    x and y can be different, if obj does some clever caching, or if there’s another thread accessing obj which changes obj.property between the two lines. So, use a temporary variable instead:

    SomeClass* a=[[SomeClass alloc] init];
    obj.property=a;
    [a release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.