• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: releasing a object containing others in a array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: releasing a object containing others in a array


  • Subject: Re: releasing a object containing others in a array
  • From: Kenneth Bruno II <email@hidden>
  • Date: Sat, 3 Oct 2009 12:18:27 -0400

As per:
<http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html >


"You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it."

If your object, at any point in its lifecycle, takes ownership of another object then it has to release that object or you'll have a potential resource leak. Usually the best place to do this is in the dealloc method of the object. When dealloc is called on your object it can run cleanup code and release anything it needs to release. The runtime will then call dealloc on any objects that need it, and so on. In this way your tree will traverse itself and deallocate any resources that need to be taken care of.

This is the great thing about following the memory management guidelines and good object oriented principles, your objects can mostly take care of themselves if you set them up properly.

Note that if you are using garbage collection most of this goes out the window. The runtime will handle most of it for you, however you might still need to use stuff like the finalize method to take care of issues other than object ownership:

<http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/GarbageCollection/Articles/gcEssentials.html >

- Ken

On Oct 3, 2009, at 4:10 AM, Nick Rogers wrote:

Hi,

I have a class as following:

@interface NodeTypeOrph : NSObject {
	int				count;
	ItemTypeOrph	*key[4];   // Warning: indexing starts at 0, not 1
	NodeTypeOrph	*branch[5];   // Fake pointers to child nodes
}

when I'll send a release to an object of this type, will it automatically release the arrays of ItemTypeOrph and NodeTypeOrph objects.
Or do I have to explicitly traverse and release?

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >releasing a object containing others in a array (From: Nick Rogers <email@hidden>)

  • Prev by Date: Re: whether to use core data...
  • Next by Date: Re: memory advice for still learning coder
  • Previous by thread: Re: releasing a object containing others in a array
  • Next by thread: objc_atomicCompareAndSwapInstanceVariable
  • Index(es):
    • Date
    • Thread