discuss-gnustep
[Top][All Lists]
Advanced

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

Re: strange crasher that puzzles me


From: David Chisnall
Subject: Re: strange crasher that puzzles me
Date: Tue, 31 Mar 2009 16:40:23 +0100

On 31 Mar 2009, at 16:12, Sebastian Reitenbach wrote:

Are there any easy rules of thumb to follow, any good documentation about
memory-management in objective-c for the "beginner" like me?
So if anyone remembers a document that made him think after reading: "ah, thats the way how it works" then you could make me happy pointing me to such
a document ;)

I wrote a short introduction here:

http://etoileos.com/dev/docs/languages/obj-c/

Simple rules of thumb:

- Any object created with +alloc or +new is retained and must be - release'd when you're done with it. The same is true of objects created with -copy and -copyWithZone:

- Any object you get by any other means is autoreleased and you should not assume that the pointer will be valid beyond the current function / method.

- If you want to keep a pointer to an object, send it a -retain message (unless you just created it with +alloc or +new).

- When you are done with an object, send it a -release message.

- If you need to return an object, but don't want to keep a reference to it, send it an -autorelease message.

- Never assume that an object you have sent a -release or -autorelease message to is still valid. Never send it any other messages. Only ever use autoreleased objects return values for functions or methods.

- Use the ASSIGN() macro when assigning an object to an ivar, with these exceptions:
        - You just created the object, using -copy, +new, or +alloc.
        - The object has a pointer to this object (this would create a loop).

- Always implement -dealloc in your objects if they have any object ivars, but never call it directly.

I think this covers pretty much everything,

David




reply via email to

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