Ok so I keep getting this error in the debugging terminal and the problem is that whatever I write it keeps equaling 0, when I press the equals button.
GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul 1 10:50:06 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 12213.
Fraction_Calculator(12213,0xad2582c0) malloc: *** error for object 0x4b88f20: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
myCalc is the only one released in the dealloc method so I don't get what it is trying to release
Here is the code please provide some pointers if you recognize the error, or else I guess I will just have to start from scratch and try to run through the whole code with the book again:
Thanks!
#import <UIKit/UIKit.h>
#import "Calculator.h"
@interface Fraction_CalculatorViewController : UIViewController {
UILabel *display;
char op;
NSMutableString *displayString;
int currentNumber;
BOOL firstOperand, isNumerator;
Calculator *myCalc;
}
@property (nonatomic, retain) IBOutlet UILabel *display;
@property (nonatomic, retain) IBOutlet NSMutableString *displayString;
-(void) ProcessDigit: (int) digit;
-(void) ProcessOperator: (char) theOp;
-(void) storeFracPart;
// Numeric keys
-(IBAction) clickDigit: (id) sender;
// Operator keys
-(IBAction) clickPlus: (id) sender;
-(IBAction) clickMinus: (id) sender;
-(IBAction) clickMultiply: (id) sender;
-(IBAction) clickDivide: (id) sender;
// Misc. keys
-(IBAction) clickEquals: (id) sender;
-(IBAction) clickClear: (id) sender;
-(IBAction) clickOver: (id) sender;
@end
#import "Fraction_CalculatorViewController.h"
@implementation Fraction_CalculatorViewController
@synthesize displayString, display;
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
isNumerator = YES;
firstOperand = YES;
self.displayString = [NSMutableString stringWithCapacity: 40];
myCalc = [[Calculator alloc] init];
[super viewDidLoad];
}
-(void) ProcessDigit: (int) digit
{
currentNumber = currentNumber * 10 + digit;
[displayString appendString: [NSString stringWithFormat: @"%i", digit]];
[display setText: displayString];
}
// Numeric keys
-(IBAction) clickDigit: (id) sender
{
int digit = [sender tag];
[self ProcessDigit: digit];
}
-(void) ProcessOperator: (char) theOp
{
NSString *opStr;
op = theOp;
switch (theOp) {
case '+':
opStr = @" + ";
break;
case '-':
opStr = @" - ";
break;
case '*':
opStr = @" × ";
break;
case '/':
opStr = @" ÷ ";
break;
}
[self storeFracPart];
[displayString appendString: opStr];
[display setText: displayString];
}
-(void) storeFracPart
{
if (firstOperand) {
if (isNumerator) {
myCalc.operand1.numerator = currentNumber;
myCalc.operand1.denominator = 1; // e.g 3 ÷ 4/5
}
else
myCalc.operand1.denominator = currentNumber;
}
else if (isNumerator) {
myCalc.operand2.numerator = currentNumber;
myCalc.operand2.denominator = 1; // e.g 3 ÷ 4/5
}
else {
myCalc.operand2.denominator = currentNumber;
firstOperand = YES;
}
currentNumber = 0;
}
-(IBAction) clickOver: (id) sender
{
[self storeFracPart];
isNumerator = NO;
[displayString appendString: @"/"];
[display setText: displayString];
}
// Arithmetic operation keys
-(IBAction) clickPlus: (id) sender
{
[self ProcessOperator: '+'];
}
-(IBAction) clickMinus: (id) sender
{
[self ProcessOperator: '-'];
}
-(IBAction) clickMultiply: (id) sender
{
[self ProcessOperator: '*'];
}
-(IBAction) clickDivide: (id) sender
{
[self ProcessOperator: '/'];
}
// Misc keys
-(IBAction) clickEquals: (id) sender
{
[self storeFracPart];
[myCalc performOperation: op];
[displayString appendString: @" = "];
[displayString appendString: [myCalc.accumulator convertToString]];
[display setText: displayString];
currentNumber = 0;
isNumerator = YES;
firstOperand = YES;
[displayString setString: @""];
}
-(IBAction) clickClear: (id) sender
{
currentNumber = 0;
isNumerator = YES;
firstOperand = YES;
[myCalc clear];
[displayString setString: @""];
[display setText: displayString];
}
- (void)dealloc
{
[myCalc release];
[super dealloc];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end