Reputation: 14040
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.
Upvotes: 1
Views: 290
Reputation: 162722
[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:
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
Upvotes: 6
Reputation: 38138
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.
Upvotes: 1
Reputation: 12003
[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.
Upvotes: 1