PDA

View Full Version : dealloc




MDMstudios
May 31, 2008, 10:43 PM
Hello everyone, I have a question for yall.
Say I have a some of IBOutlets like this.

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?



HiRez
May 31, 2008, 11:11 PM
Hello everyone, I have a question for yall.
Say I have a some of IBOutlets like this.

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?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.

Sayer
Jun 1, 2008, 10:14 AM
Also be aware that method names with init* will need to be explicitly released (dealloc) when you are done with them.

Example:

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.

Darkroom
Jun 1, 2008, 11:27 AM
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

crees!
Jun 1, 2008, 12:02 PM
Also be aware that method names with init* will need to be explicitly released (dealloc) when you are done with them.

Example:

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.

Does this change when using garbage collection?

Darkroom
Jun 1, 2008, 12:21 PM
*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)

Cromulent
Jun 1, 2008, 12:32 PM
*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)

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.

Darkroom
Jun 1, 2008, 12:35 PM
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.

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

ppc_michael
Jun 1, 2008, 09:44 PM
*i think* retain and release statements are not needed if garbage collection is turned on

That is correct. Release and retain is ignored when GC is on. This limits your application to 10.5, Intel or PPC.