Reputation: 1090
UIAlertView* av = [UIAlertView alloc];
int a = [self somefunc];
if (a == 1)
{
[[av initWithTitle:nil message:@"test msg 1" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
else if (a == 2)
{
[[av initWithTitle:nil message:@"test msg 2" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
[av release];
when I run anlyze on this code i am getting the error "refence counted object is used after it is released" at line [av release];
can I know, where av got released, does show function of UIAlertView released av?
strangly the below code doesnt show any error when analyze tool is used
if (a == 1)
{
UIAlertView* av = [[UIAlertView alloc] initWithTitle:nil message:@"test msg 1" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[av release];
}
else if (a == 2)
{
UIAlertView* av = [[UIAlertView alloc] initWithTitle:nil message:@"test msg 1" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[av release];
}
Upvotes: 0
Views: 1017
Reputation: 540105
You must always use the return value of any init
call, because an init
function can return a different value. Therefore, if you really want to separate alloc
and init
then you have to do it like this:
UIAlertView *av = [UIAlertView alloc];
// ...
av = [av initWithTitle:...]; // Might change the value of av !!
[av show];
[av release];
The "spurious release" in your code happens here:
[av initWithTitle:...]
because that might (as explained above) release av
and return a different object.
Upvotes: 1
Reputation: 1181
show
function is not releasing the UIAlertView
if it is the question here (in the second code).
Upvotes: 0
Reputation: 372
In your first code the object "av" is not sure to be initialized, what if the value of a is not 1 0r 2? av will not be initialized so when your release it there will be some problems.
In your second code av's scope became more specific or local to the conditions of if and else. Thats why xcode is sure that av will be initialized and it is safe to relese av.
Upvotes: 1