YosiFZ
YosiFZ

Reputation: 7900

UIActionSheet release after dismiss

I am using UIActionSheet in my project:

self.actionSheet = [[[UIActionSheet alloc] initWithTitle:nil] autorelease];

This is the Object:

@property (nonatomic, retain) UIActionSheet *actionSheet;

And after the UIActionSheet is dismissed I want to release it and make it nil with this code:

[self.actionSheet release];
self.actionSheet = nil;

But when I do it the app crashes, any idea why it happens?

Upvotes: 0

Views: 71

Answers (1)

Léo Natan
Léo Natan

Reputation: 57040

Manually calling release is the problem. When using the synthesized setter of a property, it properly releases the previous object.

You should either change your code to:

self.actionSheet = nil;

or, if you prefer to manually release, set the underlying instance variable to nil, like so:

[self.actionSheet release];
_actionSheet = nil;

As suggested in the comments, you should migrate your code to ARC. If you cannot migrate all of the code, you can still migrate most of it and only keep the "problematic" code in MRC.

Upvotes: 2

Related Questions