dealloc

Discussion in 'Mac Programming' started by MDMstudios, May 31, 2008.

  1. MDMstudios macrumors member

    Joined:
    Mar 18, 2008
    #1
    Hello everyone, I have a question for yall.
    Say I have a some of IBOutlets like this.

    Code:
    IBOutlet NSTextField text1;
    IBOutlet NSTextField text2;
    IBOutlet NSTextField text3;
    Should put these items in the dealloc method? Or since I am not allocating them should I just leave them?
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    Right, don't release anything you don't retain. In the case of nib objects, the runtime allocates them for the nib, so it will take care of disposing of them too.
     
  3. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #3
    Also be aware that method names with init* will need to be explicitly released (dealloc) when you are done with them.

    Example:

    Code:
    movie = [[QTMovie alloc] initWithFile:movieFileToPlayPath error:nil]; // these are NOT placed into the autorelease pool
    	
    movie = [QTMovie movieWithFile:movieFileToPlayPath error:nil]; // these are placed into the autorelease pool
    The first line needs to be released with additional code you write, the second line will be released automatically.

    This can be a source of apparently random crashes later on as an object you expected to be there had been automatically freed by the framework.
     
  4. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    ohh, i just read about this in my dev book.

    new, alloc, init, or copy should be released, as they retain themselves when created, but everything else concerning memory management is taken care of automatically in the autorelease pool...

    at least i think that's what i read - but it was a touch confusing, so maybe i'm either only half right or totally wrong. :p
     
  5. crees! macrumors 68000

    crees!

    Joined:
    Jun 14, 2003
    Location:
    MD/VA/DC
    #5
    Does this change when using garbage collection?
     
  6. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #6
    *i think* retain and release statements are not needed if garbage collection is turned on... but if garbage collection is turned on, the app's deployment target will be SDK 10.5 in which case i'm pretty sure you will not be able to make a universal binary app (so it'll be only compatible with Mac OS 10.5, and will not work with Mac OS 10.4 or earlier)
     
  7. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    Universal binaries have nothing to do with the OS version. A universal binary is one which has two executables, one compiled for Intel machines, the other for PPC machines. Universal binaries are available on both Mac OS X Tiger and Leopard. Targetting 10.5 still enables you to build universal binaries.
     
  8. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #8
    woops! hah... you're absolutely right... what i meant to say then is that garbage collection will only work for apps designed for 10.5... the end :D
     
  9. ppc_michael Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #9
    That is correct. Release and retain is ignored when GC is on. This limits your application to 10.5, Intel or PPC.
     

Share This Page