Re: [pool release] error
Re: [pool release] error
- Subject: Re: [pool release] error
- From: Roland King <email@hidden>
- Date: Sat, 27 Dec 2008 00:38:45 +0800
[decimalNumber release];
decimalNumber = [tempNum decimalNumberByDividingBy:tempDenom];
that releases the earlier retained decimalNumber
then replaces it with one from a convenience method
decimalNumberByDividingBy: which doesn't have a retain count (not
alloc, not copy), so when you dealloc later, that one gets two releases.
Since youre decimalNumber is retained, you need to have [ [ tempNum
decimalNumberByDividingBy:tempDenom ] retain ]
to keep the count on the new one.
On Dec 27, 2008, at 12:29 AM, Steve Wetzel wrote:
Does anyone know why the follow code produces an error on [pool
release]?. I get the following error :
"*** -[NSDecimalNumber release]: message sent to deallocated
instance 0x10c310". How am I overreleasing anything
#import "Fraction.h"
@implementation Fraction
-(id)init {
if(self = [super init]) {
numerator = [[NSNumber alloc] initWithInt:0];
denominator = [[NSNumber alloc] initWithInt:1];
decimalNumber = [[NSDecimalNumber alloc] initWithString:@"0"];
}
return self;
}
-(void)dealloc {
[numerator release];
[denominator release];
[decimalNumber release];
[super dealloc];
}
-(void)updateDecimalNumberWithFraction {
NSDecimalNumber *tempNum = [NSDecimalNumber decimalNumberWithString:
[numerator stringValue]];
NSDecimalNumber *tempDenom = [NSDecimalNumber
decimalNumberWithString:[denominator stringValue]];
tempNum = [NSDecimalNumber decimalNumberWithString:[numerator
stringValue]];
tempDenom = [NSDecimalNumber decimalNumberWithString:[denominator
stringValue]];
[decimalNumber release];
decimalNumber = [tempNum decimalNumberByDividingBy:tempDenom];
tempNum = [[NSDecimalNumber alloc] init];
tempDenom = [[NSDecimalNumber alloc] init];
}
-(void)setNumerator:(NSInteger)n {
[numerator release];
numerator = [NSNumber numberWithInteger:n];
if([denominator intValue] != 0) {
[self updateDecimalNumberWithFraction];
}
}
-----------
// main.m
#import <Cocoa/Cocoa.h>
#import "Fraction.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *f1 = [[Fraction alloc] init];
NSInteger n1, d1;
n1 = 1;
d1 = 2;
[f1 setNumerator:n1];
[f1 setNumerator:n1];
[f1 release];
[pool release];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden