views:

159

answers:

3

I am releasing things but the memory seems to still be there according to instruments. Am I just releasing the pointer and not the actual data? How do I release THE DATA in objective-c? What's the difference between [anObject release] or [&anObject release]???? Please excuse my lack of knowledge on the subject of memory and pointers.

+1  A: 
[anObject release];

Tells the object to decrement its retain count by 1. Every time you alloc or copy an object, you are returned an object with a retain count of one. Then, if you retain it again, the count increments. Or if you use a retain property, again, it goes up.

Calling release, like I said, decrements this count. When the count reaches zero, the object gets sent dealloc which (through calls to [super dealloc]) will eventually deallocate all the memory used for that object.

There's also autorelease, but I'll leave that for you to discover. You should read the Memory Management docs. Study them well.

jbrennan
+1  A: 

Typically, memory allocators don't return memory to the operating system immediately after release (or even ever). Thus, if the instrumentation you're looking at uses OS-level statistics, it won't see usage go down even after you've released some dynamically allocated object.

Novelocrat
RexOnRoids
I don't know. I've never written a line of Objective-C in my life.
Novelocrat
+3  A: 
[anObject release];

Specifically, you are telling the object to decrement its retain count by 1. If it happens to fall to zero -- which it might not for a variety of reasons -- then the Objective-C runtime will invoke the method -dealloc which will then invoke -release on all of the object type instance variables (and free() on malloc'd memory, if any).

There is no magic here. release is just a method call like any other method call. Given the question, I would suggest you read this:

http://developer.apple.com/IPhone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html

And, once you have dispelled the mysticism of calling methods, read this to fully understand memory management on the iPhone:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

bbum
Yes, I'll read the documentation. Thanks
RexOnRoids
Cool -- do so. The conceptual guides truly are an awesome place to start.
bbum