Skip to content

Objective-C Memory Management, The Rules

Objective-C Ref CountingInterfacelab.com: “There are only 4 basic rules when it comes to memory management in Objective-C:

If you own it, release it.
If you don’t own it, don’t release it.
Override dealloc in your classes to release the fields that you own.
Never call dealloc directly.

That’s it. The first two are the most important and we’ll get into the basics of object ownership next.”

Just an FYI for those coming from another platform, or language. Remember, Objective-C’s parent is plain ole C, and you can mix C and C++ with your Objective-C. Keep the alloc/free, new/delete rules in mind when allocating objects. Also remember Objective-C objects are reference counted objects. If you want to keep an object around bump its retain count by doing…

anObject = [object retain];

When you’re done make sure you do

[object release];

Otherwise you’ll have a leak in your code.

I’d recommend a quick look at Cocoa Dev Central’s Learn Objective-C tutorial. It’s very good.

Once you have the rules figured out it’s fairly straight forward.

Categories: Development.

Tags: ,

Comment Feed

One Response



Some HTML is OK

or, reply to this post via trackback.

Continuing the Discussion

  1. [...] counting is a pain in the keister, until you understand the rules. ARC is going to become an important tool in the quest to creating error free code. If you’ve [...]

Generic drug available for toprol xl
Benicar generic replacement
Vastarel 35mg
Aproximate priceing for lasix eye op
Tricor 145mg
Generic aricept available
300mg seroquel
Strattera 10mg
Places you can buy dostinex
Clomid order
Yaz birth control buy
Buy clomid without a prescription
Buy lamisil online
Cost of cleocin suppositories
Buy lamisil on line
How do you know if your partner is cheating
Catching cheating girlfriend
How do you catch a cheater
Catch husband cheating online
Cheap ultram online
Generic online ultram
Ultram 50 mg po q6h cost
Purchase ultram without prescription
gps tracker cheating spouse
husband is a cheater
cheating spouse spy
when your husband cheats
Strattera 40mg
Purchase strattera online
Strattera 25 mg
Purchase strattera
Strattera 25 mg
Strattera 60mg
Strattera prescribing information
how to catch spouse cheating
signs of infidelity
generic cialis
buy tramadol online
buy synthroid online
order tramadol online
buy vibramycin
buy toradol
buy lidocaine
buy celebrex
buy flagyl
lasix furosemide
canadian family pharmacy
cialis online
tramadol 100mg
nexium generic
buy nolvadex
how to catch a cheating wife
generic viagra
Canadian Family Pharmacy
buy nolvadex