سؤال

EDIT: Solved the problem myself. Turned out it was a leftover in the dealloc method that caused a UIButton to be released twice...

I'm trying to display a UIViewController on top of another UIViewController like a popup. Problem is that the view seems to be getting overreleased. With NSZombieEnabled, I get the following error:

[CALayer release]: message sent to deallocated instance 0x784bf40

I use this code to add the view:

//self.someViewController is declared as (nonatomic, retain)
self.someViewController = [[[SomeViewController alloc] initWithDelegate:self] autorelease];
[self.view addSubview:self.someViewController.view];

Then later on, I remove the view like this:

[self.someViewController.view removeFromSuperview];
self.someViewController = nil;
هل كانت مفيدة؟

المحلول

Should earlier comments not solve this perhaps this may help. I'm assumning you've created your someViewController property like this

@property (nonatomic, retain) NSViewController* someViewController;

in which case I believe your code is correct (at least I can see how it should work) and you might be seeing a secondary crash here.

I.e. when you're calling

self.someViewController = nil;

this should be freeing the memory up immediately (assuming a frame has gone by where the VC exists so the autoreleased count has already been decreased). Therefore if you have ANOTHER object being used in that someViewController VC who still exists and has a delegate set to your someViewController object and is doing a background task it will cause a crash when it trys to call back to your now deallocated object. (If you don't free your VC you wouldn't see this crash)

For example if you have a MKMapKit being displayed in someViewController and the delegate is set to someViewController... if you have implemented the method in someViewController

mapViewDidFinishLoadingMap:(MKMapView*)mapView

the MKMapKit may still be calling this from another thread if you haven't destroyed MKMapView object before yours.

I would always set other object delegates pointing to your VC (like MKMapView) to nil before destroying the said VC it uses to avoid this risk. For PDF rendering (CALayer?) you may find you need to explicitly release the object too given this uses a different memory alloc/free paradigm.

نصائح أخرى

Solved the problem myself. Turned out it was a leftover in the dealloc method that caused a UIButton to be released twice...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top