When does dealloc get called objective c
Is dealloc guaranteed to be called on the same thread that created a NSObject instance? For example, if I call [[MyFluffyBunny alloc] init] on the main thread, is dealloc guaranteed to also be called on the main thread, or can it be called on any thread once MyFluffyBunny is no longer retained?
I see sporadic crashes in my app that points to that it's not guaranteed, but I've been unable to find any documentation confirming it. The object is deallocated on whatever thread releases the last strong reference to it. That is, whatever thread calls -release the final time. It is actually during that -release call that the object is deallocated. The documentation for the -release method in the NSObject protocol says:.
The Advanced Memory Management Programming Guide: Practical Memory Management article includes this among the reasons to not use -dealloc to manage scarce resources :. This can easily be fatal for resources that should only be touched from one thread. Login using GitHub Register. Ask a Question. Please log in or register to add a comment. Your problem is absolutely the timer.
According to the NSTimer documentation, an active timer holds a retain on its target object. As a result, your controller cannot get dealloc'd while the timer is active. That in itself is a bug in your architecture, since from your -dealloc method it's obvious you're expecting the view controller to be dealloc'd while the timer is active.
But in the case of the webview, it's causing another problem. Specifically, in the middle of your -webViewDidFinishLoad: method you're canceling your timer. This causes it to release its target, and since it was the only owner of your view controller, the view controller immediately deallocs. Who calls the dealloc method and when in Objective C?
Asked 2 Months ago Answers: 5 Viewed 77 times. In general, just use nil. Three ways to dealloc 1. Rarely it will be a different object, because memory have been reused to create a new object. A crash with undefined selector because it points to a valid object which doesn't have that method. A successful method execution because the new object has a method by the same name.
Which one is best? How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is dealloc in objective C Ask Question. Asked 11 years, 4 months ago.
Active 4 years, 7 months ago. Viewed 22k times. Improve this question. Questions Questions Add a comment. Active Oldest Votes.
Improve this answer. John Franklin John Franklin 4, 1 1 gold badge 21 21 silver badges 27 27 bronze badges. You can use commas to separate instructions on a line. Is it any better? Nope, just more confusing. Its the comma operator a , b which results in the result of the right expression if you'd use the value somewhere, e. Abizern: that's a bad idea. Use the semicolon, that's wqhat it's for.
It's pointless setting foo and bar to nil in dealloc since after [super dealloc] the object they are in is gone. Yes, release all the properties of the object that are still retained. It will then send these three messages: [arr release]; [str release]; [mutableArr release]; To each of the three objects.
0コメント