bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Fix NSMenu retainCount problem


From: Fred Kiefer
Subject: Re: [PATCH] Fix NSMenu retainCount problem
Date: Sat, 07 Feb 2004 00:30:47 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030821

Quentin Mathé wrote:
Le 6 févr. 04, à 02:43, Quentin Mathé a écrit :
Le 6 févr. 04, à 01:25, Fred Kiefer a écrit :

  menu = [[NSMenu alloc] initWithTitle: @"boum"];

  NSLog(@"menu retain count before = %d", [menu retainCount]);
  {
      CREATE_AUTORELEASE_POOL(pool2);
      menuItem = [[NSMenuItem alloc] init];
      [menu addItem: menuItem];
      RELEASE(menuItem);
      RELEASE(pool2);
  }
  NSLog(@"menu retain count after = %d", [menu retainCount]);

For me this results in a menu with a retain count of one, so it will be deallocated after the next release. GNUSteop might need and internal autorelease pool in one of the NSMenu methods


For me too. but...
It's more complex than that. The autoreleasepool in the block is just showing that the notifications involve the problem we are talking about. The problem will rise again when we will have some notifications pushed back by using _notifications variable.


Here is an example :

  menu = [[NSMenu alloc] initWithTitle: @"boum"];
  [menu setMenuChangedMessagesEnabled:NO];

  {
      CREATE_AUTORELEASE_POOL(pool2);
      menuItem = [[NSMenuItem alloc] init];
      [menu addItem: menuItem];
      RELEASE(menuItem);
      RELEASE(pool2);
  }
  NSLog(@"menu retain count after = %d", [menu retainCount]);

With this example, NSLog will show a retain count which is superior to 1 (because _notifications variable will be used to retain notifications created to be executed later). We need to fix that and I hope you understand that your solution with an extra autorelease pool is a hack which doesn't solve the problem entirely.

In other terms, I just want to have with my GNUstep system the code below exits on the third NSLog with the output :
2004-02-06 18:37:51.831 MyProgram[433] Menu1 1 :
2004-02-06 18:37:51.879 MyProgram[433] Menu2 1 :
MyProgram has exited due to signal blabla

Here is the code I'm talking about :

int main(int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Youlà"];
    [menu setMenuChangedMessagesEnabled:NO];

    NSLog(@"Menu1 %d :", [menu retainCount]);
[menu addItem:[[NSMenuItem alloc] initWithTitle:@"You" action:NULL keyEquivalent:@""]];
    NSLog(@"Menu2 %d :", [menu retainCount]);
    [menu release];
    NSLog(@"Menu3 %d :", [menu retainCount]);

    [pool release];

    return NSApplicationMain(argc, argv);
}


Hi Quentin,

if what you want is the actuall behaviour on MacOSX, than we really need your full patch to get it. What I was aiming at, was to have a correct reference count on the long run (after another release of an autorelease pool), but if we need it locally, than yours is the only way I see. As this is a bigger change I would like to hear another opinion. There will be dozens of other places in the the GNUstep code, that will need similar cleanup, but it is surely worth the effort and I would go for it.

Fred





reply via email to

[Prev in Thread] Current Thread [Next in Thread]