I want to ask about the iPhone application and objective C question. In the implementation program, there are function called 'dealloc', does this function only be called one time by the application?
For example, if I alloc a object and retain it 2 times, the retains count is 2 and I never use 'release' in the program, unless in the dealloc. Will the object be removed from the memory, or the objective will be removed from the memory. Thank you.
In the implementation program, there are function called 'dealloc', does this function only be called one time by the application?
Yes. -dealloc destroys the object. Trying to send any message to it again, including -dealloc is an error.
if I alloc a object and retain it 2 times, the retains count is 2
Careful. The retain count is at least 3. Other things than your code might retain the object. It's better not to worry to much about retain counts and only think in terms of ownership.
Each alloc, new, copy or retain is an claim of ownership. The object's dealloc method will only be called when all claims of ownership have been relinquished. A claim of ownership is relinquished by sending -release. So if you never release an object except in its own dealloc, you'll never release it.
dealloc is called once by the system when the object is destroyed (when its reference count reaches 0). If you have member variables in your class that you alloc in your init function, you must release them in your dealloc function.
If you give someone a pointer to one of those member objects and they retain it, the member could survive the release in your dealloc, but by sending a retain message they are taking responsibility for sending a release message later, ensuring its eventual destruction.
Related
I have created an object using alloc/init method, and after I release it -dealloc should be called immediately as per documentation. I set a breakpoint on -dealloc method but it isn't hit, and my -dealloc method is not called.
Please tell me what is the reason behind that, and what is use of dealloc method in objective c ?
The -dealloc method is not always called when you expect it to be called. The runtime might also have issued a -retain on your object for internal reasons.
It's also possible that you have (directly or indirectly) caused an extra -retain to be issued. If the retains/allocs and releases are not balanced, you'll never see -dealloc called. It helps to turn on the Static Analyzer, to make sure your calls balance.
Just follow the memory management rules, don't second guess the runtime, and let the memory management system do its job.
The answers to When does dealloc method call? may help you understand what you're seeing.
because it still has reference. that means its reference count not reached to zero. i don't know your code, where it is referencing. but it is not calling that means somehow still it has reference. it may be because of strong relationship or parent-child relationship
all Objective-C objects are allocated on the heap, so they must
therefore be deallocated somewhere if you are not to run out of
resources.
This gave way to the reference counting method, which is still used
today: each object keeps count of any references held to it. If you
receive an object and you want to keep it, you retain that object,
incrementing its reference count. When you are done with it, you
release it, which decrements its reference count. Once that count
reaches zero, it is inferred that no one is referencing the object and
it is automatically deallocated using the -dealloc method.
Additionally, an object could be told to “release at some point in the
(hopefully) near future” using autorelease pools. The idea is that
somewhere on the stack (typically at the start of a thread or while
responding to input events) an autorelease pool is created and pushed
onto a stack. Any object can then be sent an -autorelease message, and
it is assigned to that pool.
When the pool object is deallocated, it simply sends a -release
message to all its assigned objects. That way, any objects that are no
longer used (i.e. they haven’t been explicitly retained) are then
deallocated.
The dealloc is called (at more cases) whenever your object is released. You can't directly call this method.
#interface myViewController:UIViewController
{
NSString *myStr;
}
#end
Here the dealloc method in the #implementation of myViewController will be called (at most cases) when the myViewController object is released, not when myStr is released.
Although you don't have to use if you ARC.
Why is my object in the following leak trace doesn't get released?
The trace says its reference count is 0, so why doesn't it get released?
The object is a custom class that derives directly from NSObject. all I do with it is alloc it, init it, copy some strings/numbers from it, and send release, but still its considered a leak and doesn't get deallocated. I see it under allocations in instruments as 'living' so its really not deallocated. I create hundreds of these objects, so I cannot allow them to live.
How can I make this object get deallocated? why isn't it deallocated in the first place?
Well, it looks like you forgot to call [super dealloc] in your -dealloc method. We've all done that. :)
So the object is getting the dealloc call as you would expect, but isn't actually being deallocated.
Can someone explain the differences between free(), release, autorelease and dealloc?
free() is a C function that you use to dispose of no longer needed memory you obtained through malloc() or other function that is documented to require free() to deallocate its memory e.g. strdup().
-dealloc is an Objective-C selector that is sent by the Objective-C runtime to an object when the object is no longer owned by any part of the application.
-release is the selector you send to an object to indicate that you are relinquishing ownership of that object. Once an object is not owned by anybody, it is sent the -dealloc message by the runtime.
-autorelease is a selector you send to an object to indicate you are relinquishing ownership of the object. However if nobody now owns the object the final -dealloc will be deferred until some unspecified later point. In fact, what you are really doing is transferring your ownership to an autorelease pool which will then release it when it is itself released (or drained).
You must never send -dealloc to an object except to super in the object's own -dealloc method.
Out of the 4 methods you've mentioned you'll typically only use release and autorelease in objective-c. free maybe used when you've used c and malloc otherwise you should use release and autorelease.
dealloc shouldn't be called by any of your code as it can interfere with the retain/release cycle that objective-c uses to keep track of resources. Using dealloc will almost certainly result in crashes at runtime if you ever use it.
The ultimate resource of the use of the release and autorelease is the Apple memory management documentation
free is the opposite of malloc and is used in C. You'll likely not use it very much programming in Objective C
If you own an object, you release it when you're done with it
You can also autorelease an object. This automatically releases it at the end of the current run loop iteration
When the reference count on an object drops to zero, the runtime calls dealloc. You should not call this method yourself
You "own" an object if you alloc, new, retain or copy it.
Apple provide some good documentation on this.
I recently took an objective-c test to see how I would do.
Turns out my score wasn't anywhere near as good as I hoped. That means more studying.
During the test, I was asked this question:
How do you free an object?
A. [obj dealloc];
B. [obj release];
C. None of the above
My choice was A, and I don't know if it's correct. The question is confusing: Doesn't release call dealloc, therefore achieving the same result?
No. release decrements the object's reference count.
You don't call dealloc directly. Call release to decrement the reference count and let the runtime call dealloc when the reference count becomes zero.
Yes, release does call dealloc but only after decrementing the reference count and only if the reference count went to zero. The NSObject class reference says that release:
"Decrements the receiver’s reference count."
"The receiver is sent a dealloc message when its reference count reaches 0."
I know an answer has already been accepted for this, but I can't resist adding my own thoughts.
The answer to the test question is technically C. You don't free objects, the runtime frees them when it thinks they are no longer in use.
If you release an object in the reference counted environment, you are not freeing it, merely indicating that you are relinquishing any claim of ownership. When nobody has an ownership claim, dealloc is called by the runtime and the object is freed. Similarly, in the garbage collected environment, when you overwrite a reference, you are signalling that you are no longer interested in it. Once all references have gone, at some indeterminate time later, finalize is sent to the object and the object is freed.
Hi i want to know the difference between drain, release,dealloc and retain in Objective-C.
retain increase the reference count on an object
release decreases the reference on an object
drain is used in place of release on ONLY for NSAutoreleasePool objects due to some arcana related to the Objective C garbage collection
dealloc is called by the system once the retainCount of an object hits 0. It is where you clean up various things your object has (like a deconstructor or finalizer). You should NEVER call it directly, except for calling [super dealloc] at the end of your dealloc routines.
You really should just read through Apple's memory management documentation.