Cocoa Help: Memory Leak

Discussion in 'Mac Programming' started by GigaRoc, Jun 5, 2008.

  1. GigaRoc macrumors newbie

    Joined:
    Jun 5, 2008
    #1
    I have a memory leak with my cocoa program. It happens at 35 seconds every time. even if i don't enter any commands. I've commented out all of my code leaving nothing but a bunch of IBOutlet NSTextfield * foo decelerations in my NSObject. And my method decelerations which all look like - (IBAction) foo:(id)sender;

    is there something I'm doing wrong. any idea's on what could cause this?

    I would post some code but it's all commented out.

    This is my first time programing cocoa. I'm a linux/Unix C programmer by trade.

    Giga
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    a) Can you post your code?

    b) Are you sure its not an object that should be auto-released that just hasn't gone yet?
     
  3. antibact1 macrumors 6502

    Joined:
    Jun 1, 2006
  4. GigaRoc thread starter macrumors newbie

    Joined:
    Jun 5, 2008
    #4
    Code:
    //
    //  Coin Jar.h
    //  Coin Jar
    //
    //  Created by Ness on 5/31/08.
    //  Copyright 2008 __MyCompanyName__. All rights reserved.
    //
    
    #import <Cocoa/Cocoa.h>
    #import "coins.h"
    @interface Coin_Jar : NSObject {
    	coins *myCoinJar;
        IBOutlet NSTextField *doCount;
    	IBOutlet NSTextField *doValue;
    	IBOutlet NSTextField *quCount;
    	IBOutlet NSTextField *quValue;
    	IBOutlet NSTextField *diCount;
    	IBOutlet NSTextField *diValue;
    	IBOutlet NSTextField *niCount;
    	IBOutlet NSTextField *niValue;
    	IBOutlet NSTextField *peCount;
    	IBOutlet NSTextField *peValue;
    	IBOutlet NSTextField *totValue;
    
    }
    - (IBAction)doInc:(id)sender;
    - (IBAction)doDec:(id)sender;
    - (IBAction)quInc:(id)sender;
    - (IBAction)quDec:(id)sender;
    - (IBAction)diInc:(id)sender;
    - (IBAction)diDec:(id)sender;
    - (IBAction)niInc:(id)sender;
    - (IBAction)niDec:(id)sender;
    - (IBAction)peInc:(id)sender;
    - (IBAction)peDec:(id)sender;
    - (void)updatePrimaryWindow;
    - (void)initWindow;
    @end
    
    Code:
    //
    //  Coin Jar.m
    //  Coin Jar
    //
    //  Created by Ness on 5/31/08.
    //  Copyright 2008 __MyCompanyName__. All rights reserved.
    //
    
    #import "Coin Jar.h"
    
    @implementation Coin_Jar
    - (IBAction)doInc:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incDollars:temp];
    }
    - (IBAction)doDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decDollars:temp];
    }
    - (IBAction)quInc:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incQuarters:temp];
    }
    - (IBAction)quDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decQuarters:temp];
    }
    - (IBAction)diInc:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incDimes:temp];
    }
    - (IBAction)diDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decDimes:temp];
    }
    - (IBAction)niInc:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incNickels:temp];
    }
    - (IBAction)niDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decNickels:temp];
    }
    - (IBAction)peInc:(id)sender
    {	
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incPennies:temp];
    }
    - (IBAction)peDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decPennies:temp];
    }
    - (void)awakeFromNib
    {
    	myCoinJar = [[coins alloc] init];
    	[self initWindow];
    }
    - (void)initWindow
    {
    	[doCount setStringValue:@"0"];
    	[doValue setStringValue:@"$ 0.00"];
    	[quCount setStringValue:@"0"];
    	[quValue setStringValue:@"$ 0.00"];
    	[diCount setStringValue:@"0"];
    	[diValue setStringValue:@"$ 0.00"];
    	[niCount setStringValue:@"0"];
    	[niValue setStringValue:@"$ 0.00"];
    	[peCount setStringValue:@"0"];
    	[peValue setStringValue:@"$ 0.00"];
    	[totValue setStringValue:@"$ 0.00"];
    }
    - (void)updatePrimaryWindow
    {
    	[doCount setStringValue:[myCoinJar printDollarNumber]];
    	[doValue setStringValue:[myCoinJar printDollarValue]];
    	[quCount setStringValue:[myCoinJar printQuarterNumber]];
    	[quValue setStringValue:[myCoinJar printQuarterValue]];
    	[diCount setStringValue:[myCoinJar printDimeNumber]];
    	[diValue setStringValue:[myCoinJar printDimeValue]];
    	[niCount setStringValue:[myCoinJar printNickelNumber]];
    	[niValue setStringValue:[myCoinJar printNickelValue]];
    	[peCount setStringValue:[myCoinJar printPennyNumber]];
    	[peValue setStringValue:[myCoinJar printPennyValue]];
    	[totValue setStringValue:[myCoinJar printTotalValue]];
    }
    
    
    @end
    
    Code:
    //
    //  Coin Jar.m
    //  Coin Jar
    //
    //  Created by Ness on 5/31/08.
    //  Copyright 2008 __MyCompanyName__. All rights reserved.
    //
    
    #import "Coin Jar.h"
    
    @implementation Coin_Jar
    - (IBAction)doInc:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incDollars:temp];
    }
    - (IBAction)doDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decDollars:temp];
    }
    - (IBAction)quInc:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incQuarters:temp];
    }
    - (IBAction)quDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decQuarters:temp];
    }
    - (IBAction)diInc:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incDimes:temp];
    }
    - (IBAction)diDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decDimes:temp];
    }
    - (IBAction)niInc:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incNickels:temp];
    }
    - (IBAction)niDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decNickels:temp];
    }
    - (IBAction)peInc:(id)sender
    {	
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar incPennies:temp];
    }
    - (IBAction)peDec:(id)sender
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:1];
    	[myCoinJar decPennies:temp];
    }
    - (void)awakeFromNib
    {
    	myCoinJar = [[coins alloc] init];
    	[self initWindow];
    }
    - (void)initWindow
    {
    	[doCount setStringValue:@"0"];
    	[doValue setStringValue:@"$ 0.00"];
    	[quCount setStringValue:@"0"];
    	[quValue setStringValue:@"$ 0.00"];
    	[diCount setStringValue:@"0"];
    	[diValue setStringValue:@"$ 0.00"];
    	[niCount setStringValue:@"0"];
    	[niValue setStringValue:@"$ 0.00"];
    	[peCount setStringValue:@"0"];
    	[peValue setStringValue:@"$ 0.00"];
    	[totValue setStringValue:@"$ 0.00"];
    }
    - (void)updatePrimaryWindow
    {
    	[doCount setStringValue:[myCoinJar printDollarNumber]];
    	[doValue setStringValue:[myCoinJar printDollarValue]];
    	[quCount setStringValue:[myCoinJar printQuarterNumber]];
    	[quValue setStringValue:[myCoinJar printQuarterValue]];
    	[diCount setStringValue:[myCoinJar printDimeNumber]];
    	[diValue setStringValue:[myCoinJar printDimeValue]];
    	[niCount setStringValue:[myCoinJar printNickelNumber]];
    	[niValue setStringValue:[myCoinJar printNickelValue]];
    	[peCount setStringValue:[myCoinJar printPennyNumber]];
    	[peValue setStringValue:[myCoinJar printPennyValue]];
    	[totValue setStringValue:[myCoinJar printTotalValue]];
    }
    
    
    @end
    
    Code:
    //
    //  coins.m
    //  Coin Jar
    //
    //  Created by Ness on 6/4/08.
    //  Copyright 2008 __MyCompanyName__. All rights reserved.
    //
    
    #import "coins.h"
    
    
    @implementation coins
    - (id)init
    {
    	NSNumber *temp = [[NSNumber alloc] initWithInt:0];
    	return [self initWithDollars:temp
    						Quarters:temp
    						   Dimes:temp
    						 Nickels:temp
    						 Pennies:temp];
    }
    - (id)initWithDollars:(NSNumber *)dollars
    			 Quarters:(NSNumber *)quarters
    				Dimes:(NSNumber *)dimes
    			  Nickels:(NSNumber *)nickels
    			  Pennies:(NSNumber *)pennies
    {
    	if (![super init])
    		return nil;
    	[Dol init];
    	Dol = dollars;
    	[Nic init];
    	Nic = nickels;
    	[Qua init];
    	Qua = quarters;
    	[Dim init];
    	Dim=dimes;
    	[Pen init];
    	Pen=pennies;
    	return self;
    }
    - (void)incDollars:(NSNumber*)incValue
    {
    	int temp;
    	temp = [Dol intValue];
    	temp=temp+[incValue intValue];
    	Dol = [NSNumber numberWithInt:temp];
    }
    - (void)incQuarters:(NSNumber *)incValue
    {
    	int temp;
    	temp = [Qua intValue];
    	temp=temp+[incValue intValue];
    	Qua = [NSNumber numberWithInt:temp];
    }
    - (void)incDimes:(NSNumber *)incValue
    {
    	int temp;
    	temp = [Dim intValue];
    	temp=temp+[incValue intValue];
    	Dim = [NSNumber numberWithInt:temp];
    }
    - (void)incNickels:(NSNumber *)incValue
    {
    	int temp;
    	temp = [Nic intValue];
    	temp=temp+[incValue intValue];
    	Nic = [NSNumber numberWithInt:temp];
    }
    - (void)incPennies:(NSNumber *)incValue
    {
    	int temp;
    	temp = [Pen intValue];
    	temp=temp+[incValue intValue];
    	Pen = [NSNumber numberWithInt:temp];
    }
    - (void)decDollars:(NSNumber *)decValue
    {
    	int temp;
    	temp = [Dol intValue];
    	temp = temp - [decValue intValue];
    	if (temp < 0)
    		temp = 0;
    	Dol= [NSNumber numberWithInt:temp];
    }
    - (void)decQuarters:(NSNumber *)decValue
    {
    	int temp;
    	temp = [Qua intValue];
    	temp = temp - [decValue intValue];
    	if (temp < 0)
    		temp = 0;
    	Qua= [NSNumber numberWithInt:temp];
    }
    - (void)decDimes:(NSNumber *)decValue
    {
    	int temp;
    	temp = [Dim intValue];
    	temp = temp - [decValue intValue];
    	if (temp < 0)
    		temp = 0;
    	Dim= [NSNumber numberWithInt:temp];
    }
    - (void)decNickels:(NSNumber *)decValue
    {
    	int temp;
    	temp = [Nic intValue];
    	temp = temp - [decValue intValue];
    	if (temp < 0)
    		temp = 0;
    	Nic= [NSNumber numberWithInt:temp];
    }
    - (void)decPennies:(NSNumber *)decValue
    {
    	int temp;
    	temp = [Pen intValue];
    	temp = temp - [decValue intValue];
    	if (temp < 0)
    		temp = 0;
    	Pen= [NSNumber numberWithInt:temp];
    }
    - (void)incAllDollars:(NSNumber *)dollars
    			 Quarters:(NSNumber *)quarters
    				Dimes:(NSNumber *)dimes
    			  Nickels:(NSNumber *)nickels
    			  Pennies:(NSNumber *)pennies
    {
    	[self incDollars:dollars];
    	[self incQuarters:quarters];
    	[self incDimes:dimes];
    	[self incNickels:nickels];
    	[self incPennies:pennies];
    }
    - (void)decAllDollars:(NSNumber *)dollars
    			 Quarters:(NSNumber *)quarters
    				Dimes:(NSNumber *)dimes
    			  Nickels:(NSNumber *)nickels
    			  Pennies:(NSNumber *)pennies
    {
    	[self decDollars:dollars];
    	[self decQuarters:quarters];
    	[self decDimes:dimes];
    	[self decNickels:nickels];
    	[self decPennies:pennies];
    }
    - (NSString *)printDollarNumber
    {
    	return [Dol stringValue];
    }
    - (NSString *)printDollarValue
    {
    	float temp = 1 * [Dol floatValue];
    	NSString* tempStr=[[NSString alloc] initWithFormat:@"$ %.02f",temp];
    	return tempStr;
    }
    - (NSString *)printQuarterNumber
    {
    	return [Qua stringValue];
    }
    - (NSString *)printQuarterValue
    {
    	float temp = .25 * [Qua floatValue];
    	NSString* tempStr=[[NSString alloc] initWithFormat:@"$ %.02f",temp];
    	return tempStr;
    }
    - (NSString *)printDimeNumber
    {
    	return [Dim stringValue];
    }
    - (NSString *)printDimeValue
    {
    	float temp = .1 * [Dim floatValue];
    	NSString* tempStr=[[NSString alloc] initWithFormat:@"$ %.02f",temp];
    	return tempStr;
    }
    - (NSString *)printNickelNumber
    {
    	return [Nic stringValue];
    }
    - (NSString *)printNickelValue
    {
    	float temp = .05 * [Nic floatValue];
    	NSString* tempStr=[[NSString alloc] initWithFormat:@"$ %.02f",temp];
    	return tempStr;
    }
    - (NSString *)printPennyNumber
    {
    	return [Pen stringValue];
    }
    - (NSString *)printPennyValue
    {
    	float temp = .01 * [Pen floatValue];
    	NSString* tempStr=[[NSString alloc] initWithFormat:@"$ %.02f",temp];
    	return tempStr;
    }
    - (NSString *)PrintTotalValue
    {	
    	float temp = 1 * [Dol floatValue];
    	temp = temp + .25 * [Qua floatValue];
    	temp = temp + .10 * [Dim floatValue];
    	temp = temp + .05 * [Nic floatValue];
    	temp = temp + .01 * [Pen floatValue];
    	NSString* tempStr=[[NSString alloc] initWithFormat:@"$ %.02f",temp];
    	return tempStr;
    }
    @end
    
    The first file is the main program header file.
    The second file is the source for the header
    the third is the class header
    the forth is the class source

    Edit: everything was commented out. i put a /* after each { and a */ before each } the only thing i didn't comment out was coinjar.h
    but i did comment out the #import "coins.h" and the deceleration of myCoinJar
     
  5. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    1. Remove all comments
    2. Delete the standard init method in coins.m
    3. In the init method for coins.m change [blah init]; to blah=[[NSNumber alloc] initWithInt:0];
    4. Where you are changing some of the stored values in the coin jar you need to not create a new NSNumber and change the existing one with [blah setIntValue:theInteger]; not blah=[NSNumber numberWithInt:theInteger];
    5. In the rest of the code replace all uses of [[NSNumber alloc] initWithInt:theInteger]; with [NSNumber numberWithInt:theInteger]; (except where I told you to add them earlier) as you are only using them in a method you are directly calling so you don't need to go through the hassle of alloc, init.
     

Share This Page