So... firstly, - tempNum = [[NSDecimalNumber alloc] init] at the end of a method after assigning an autoreleased instance of NSDecimalNumber to it - why? I'm trying to understand the logic in this and can't. As near as I can tell you aren't doing any book- keeping of this later on.

What I would imagine is the cause of the error based on my ten mile high overview is that at some point in your code you set decimalNumber to be an instance of an autoreleased object (in updateDecimalNumberWithFraction). You then release the Fraction instance in your main method which triggers this object to dealloc. Now the pool drains but the object it expects to release isn't in existence anymore. If you are going to assign it an autoreleased instance, you should explicitly -retain it. NSZombie is your friend here.

-rob.


On Dec 26, 2008, at 11: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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/rob%40pinchmedia.com

This email sent to [email protected]

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to