crash on custom NSWindow subclass dealloc

  • I have a custom subclass of NSWindow that I've defined a dealloc
    method for as so:

    - (void)dealloc
    {
    [ogl_view release];
    NSZoneFree([self zone], [self zone]);
    [super dealloc];
    }

    Things work fine until the program hits "[super dealloc];".  At that
    point, I get a crash every time and the stack looks like this:

    #0    0x0181b028 in ??
    #1    0x900052ef in free
    #2    0x9080dfc0 in CFRelease
    #3    0x932aeabe in -[NSWindow _setWindowNumber:]
    #4    0x9333d0da in -[NSWindow _termWindowIfOwner]
    #5    0x9333c2c5 in -[NSWindow dealloc]
    #6    0x004386bb in -[CocoaOpenGLWin dealloc] at cocoa_opengl_win.m:51

    I'm not sure what's going on at all.  What are the mechanics of
    deallocating a custom NSWindow subclass?  I feel I'm missing something
    crucial here because even when I pare the class down to almost
    nothing, it still crashes.

    thanks,
    wes
  • Well, I solved my problem by using the windowWillClose notification.
    I was thinking about things like I would in C++ with destructors.  I
    guess one should favor notifications in Cocoa over such designs.

    thanks,
    wes

    On Dec 19, 2007 1:19 AM, Wesley Smith <wesley.hoke...> wrote:
    > I have a custom subclass of NSWindow that I've defined a dealloc
    > method for as so:
    >
    > - (void)dealloc
    > {
    > [ogl_view release];
    > NSZoneFree([self zone], [self zone]);
    > [super dealloc];
    > }
    >
    > Things work fine until the program hits "[super dealloc];".  At that
    > point, I get a crash every time and the stack looks like this:
    >
    > #0      0x0181b028 in ??
    > #1      0x900052ef in free
    > #2      0x9080dfc0 in CFRelease
    > #3      0x932aeabe in -[NSWindow _setWindowNumber:]
    > #4      0x9333d0da in -[NSWindow _termWindowIfOwner]
    > #5      0x9333c2c5 in -[NSWindow dealloc]
    > #6      0x004386bb in -[CocoaOpenGLWin dealloc] at cocoa_opengl_win.m:51
    >
    >
    > I'm not sure what's going on at all.  What are the mechanics of
    > deallocating a custom NSWindow subclass?  I feel I'm missing something
    > crucial here because even when I pare the class down to almost
    > nothing, it still crashes.
    >
    > thanks,
    > wes
    >
  • On Dec 19, 2007 1:19 AM, Wesley Smith <wesley.hoke...> wrote:
    > I have a custom subclass of NSWindow that I've defined a dealloc
    > method for as so:
    >
    > - (void)dealloc
    > {
    > [ogl_view release];
    > NSZoneFree([self zone], [self zone]);
    > [super dealloc];
    > }

    NSZoneFree line is incorrect for a few reasons (one is your object
    doesn't own the zone it was allocated from so it shouldn't attempt to
    free that zone).

    In dealloc you are responsible for releasing any objects your object
    maintains a retain on and/or any memory your object allocated and
    owns. You are not supposed free memory of your own object in dealloc.
    Your object was sent a dealloc message by the runtime because the
    runtime is about to destroy the object and the runtime wants you clean
    up anything your object owns.

    Even C++ you don't free the object itself in the destructor.

    -Shawn
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today