Where is my object allocation and memory leak in this iPhone/objective C code?
Posted
by Spottswoode
on Stack Overflow
See other posts from Stack Overflow
or by Spottswoode
Published on 2010-04-25T19:41:48Z
Indexed on
2010/04/25
19:43 UTC
Read the original article
Hit count: 687
memory-leaks
Hello, I'm still a rookie when it comes to this programming gig and was wondering if someone could help me smooth out this code. Functionally, the code works great and does what I need it to do. But when I run the performance tool the allocation graph peaks, the CPU load is high, there's a leak(s), and I've also confirmed when running on my iPhone it seems noticeably slower then the rest of the components in my app.
I'd appreciate any advice/tips/help anyone could give me. :)
Thanks in advance!
.h file
//
// Time_CalculatorViewController.h
// Time Calculator
//
// Created by Adam Soloway on 2/19/10.
// Copyright Legacy Pilots 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Time_CalculatorViewController : UIViewController {
//BOOL moveViewUp;
//CGFloat scrollAmount;
IBOutlet UILabel *hoursLabel;
IBOutlet UILabel *minutesLabel;
IBOutlet UILabel *hoursDecimalLabel;
IBOutlet UILabel *minutesDecimalLabel;
IBOutlet UILabel *errorLabel;
IBOutlet UITextField *minTextField1;
IBOutlet UITextField *minTextField2;
IBOutlet UITextField *minTextField3;
IBOutlet UITextField *minTextField4;
IBOutlet UITextField *minTextField5;
IBOutlet UITextField *minTextField6;
IBOutlet UITextField *minTextField7;
IBOutlet UITextField *minTextField8;
IBOutlet UITextField *minTextField9;
IBOutlet UITextField *minTextField10;
IBOutlet UITextField *hourTextField1;
IBOutlet UITextField *hourTextField2;
IBOutlet UITextField *hourTextField3;
IBOutlet UITextField *hourTextField4;
IBOutlet UITextField *hourTextField5;
IBOutlet UITextField *hourTextField6;
IBOutlet UITextField *hourTextField7;
IBOutlet UITextField *hourTextField8;
IBOutlet UITextField *hourTextField9;
IBOutlet UITextField *hourTextField10;
IBOutlet UIButton *resetAll;
NSString *minutesString1;
NSString *minutesString2;
NSString *minutesString3;
NSString *minutesString4;
NSString *minutesString5;
NSString *minutesString6;
NSString *minutesString7;
NSString *minutesString8;
NSString *minutesString9;
NSString *minutesString10;
NSString *hoursString1;
NSString *hoursString2;
NSString *hoursString3;
NSString *hoursString4;
NSString *hoursString5;
NSString *hoursString6;
NSString *hoursString7;
NSString *hoursString8;
NSString *hoursString9;
NSString *hoursString10;
int hourDecimalNumber;
int totalTime;
int leftOverMinutes;
int minuteNumber1;
int minuteNumber2;
int minuteNumber3;
int minuteNumber4;
int minuteNumber5;
int minuteNumber6;
int minuteNumber7;
int minuteNumber8;
int minuteNumber9;
int minuteNumber10;
int hourNumber1;
int hourNumber2;
int hourNumber3;
int hourNumber4;
int hourNumber5;
int hourNumber6;
int hourNumber7;
int hourNumber8;
int hourNumber9;
int hourNumber10;
}
//- (void)scrollTheView:(BOOL)movedUp;
- (void)calculateTime;
- (IBAction)resetAllValues;
@end
.m file
//
// Time_CalculatorViewController.m
// Time Calculator
//
// Created by Adam Soloway on 2/19/10.
// Copyright Legacy Pilots 2010. All rights reserved.
//
#import "Time_CalculatorViewController.h"
@implementation Time_CalculatorViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if( minTextField1.editing || minTextField2.editing || minTextField3.editing || minTextField4.editing || minTextField5.editing || minTextField6.editing || minTextField7.editing || minTextField8.editing || minTextField9.editing || minTextField10.editing || hourTextField1.editing || hourTextField2.editing || hourTextField3.editing || hourTextField4.editing || hourTextField5.editing || hourTextField6.editing || hourTextField7.editing || hourTextField8.editing || hourTextField9.editing || hourTextField10.editing) {
[minTextField1 resignFirstResponder];
[minTextField2 resignFirstResponder];
[minTextField3 resignFirstResponder];
[minTextField4 resignFirstResponder];
[minTextField5 resignFirstResponder];
[minTextField6 resignFirstResponder];
[minTextField7 resignFirstResponder];
[minTextField8 resignFirstResponder];
[minTextField9 resignFirstResponder];
[minTextField10 resignFirstResponder];
[hourTextField1 resignFirstResponder];
[hourTextField2 resignFirstResponder];
[hourTextField3 resignFirstResponder];
[hourTextField4 resignFirstResponder];
[hourTextField5 resignFirstResponder];
[hourTextField6 resignFirstResponder];
[hourTextField7 resignFirstResponder];
[hourTextField8 resignFirstResponder];
[hourTextField9 resignFirstResponder];
[hourTextField10 resignFirstResponder];
[self calculateTime];
//if (moveViewUp) [self scrollTheView:NO];
}
[super touchesBegan:touches withEvent:event];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[minutesString1 release];
[minutesString2 release];
[minutesString3 release];
[minutesString4 release];
[minutesString5 release];
[minutesString6 release];
[minutesString7 release];
[minutesString8 release];
[minutesString9 release];
[minutesString10 release];
[hoursString1 release];
[hoursString2 release];
[hoursString3 release];
[hoursString4 release];
[hoursString5 release];
[hoursString6 release];
[hoursString7 release];
[hoursString8 release];
[hoursString9 release];
[hoursString10 release];
[super dealloc];
}
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
//[minTextField10 resignFirstResponder];
//if (moveViewUp) [self scrollTheView:NO];
[self calculateTime];
return YES;
}
- (IBAction)resetAllValues
{
minTextField1.text = 0;
minTextField2.text = 0;
minTextField3.text = 0;
minTextField4.text = 0;
minTextField5.text = 0;
minTextField6.text = 0;
minTextField7.text = 0;
minTextField8.text = 0;
minTextField9.text = 0;
minTextField10.text = 0;
hourTextField1.text = 0;
hourTextField2.text = 0;
hourTextField3.text = 0;
hourTextField4.text = 0;
hourTextField5.text = 0;
hourTextField6.text = 0;
hourTextField7.text = 0;
hourTextField8.text = 0;
hourTextField9.text = 0;
hourTextField10.text = 0;
totalTime = 0;
leftOverMinutes = 0;
hoursLabel.text = [NSString stringWithFormat:@"0"];
hourDecimalNumber = 0;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
minutesDecimalLabel.text = [NSString stringWithFormat:@"0"];
self.calculateTime;
}
- (void)calculateTime {
minutesString1 = minTextField1.text;
minutesString2 = minTextField2.text;
minutesString3 = minTextField3.text;
minutesString4 = minTextField4.text;
minutesString5 = minTextField5.text;
minutesString6 = minTextField6.text;
minutesString7 = minTextField7.text;
minutesString8 = minTextField8.text;
minutesString9 = minTextField9.text;
minutesString10 = minTextField10.text;
hoursString1 = hourTextField1.text;
hoursString2 = hourTextField2.text;
hoursString3 = hourTextField3.text;
hoursString4 = hourTextField4.text;
hoursString5 = hourTextField5.text;
hoursString6 = hourTextField6.text;
hoursString7 = hourTextField7.text;
hoursString8 = hourTextField8.text;
hoursString9 = hourTextField9.text;
hoursString10 = hourTextField10.text;
minuteNumber1 = [minutesString1 intValue];
minuteNumber2 = [minutesString2 intValue];
minuteNumber3 = [minutesString3 intValue];
minuteNumber4 = [minutesString4 intValue];
minuteNumber5 = [minutesString5 intValue];
minuteNumber6 = [minutesString6 intValue];
minuteNumber7 = [minutesString7 intValue];
minuteNumber8 = [minutesString8 intValue];
minuteNumber9 = [minutesString9 intValue];
minuteNumber10 = [minutesString10 intValue];
hourNumber1 = ([hoursString1 intValue] * 60);
hourNumber2 = ([hoursString2 intValue] * 60);
hourNumber3 = ([hoursString3 intValue] * 60);
hourNumber4 = ([hoursString4 intValue] * 60);
hourNumber5 = ([hoursString5 intValue] * 60);
hourNumber6 = ([hoursString6 intValue] * 60);
hourNumber7 = ([hoursString7 intValue] * 60);
hourNumber8 = ([hoursString8 intValue] * 60);
hourNumber9 = ([hoursString9 intValue] * 60);
hourNumber10 = ([hoursString10 intValue] * 60);
totalTime = (hourNumber1 + hourNumber2 +hourNumber3 +hourNumber4 +hourNumber5 +hourNumber6 +hourNumber7 +hourNumber8 +hourNumber9 +hourNumber10 + minuteNumber1 + minuteNumber2 + minuteNumber3 + minuteNumber4 + minuteNumber5 +minuteNumber6 + minuteNumber7 + minuteNumber8 + minuteNumber9 + minuteNumber10);
if (totalTime <= 59) {
leftOverMinutes = totalTime;
hoursLabel.text = [NSString stringWithFormat:@"0"];
hourDecimalNumber = 0;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >59 && totalTime <= 119){
leftOverMinutes = totalTime - 60;
hoursLabel.text = [NSString stringWithFormat:@"1"];
hourDecimalNumber = 1;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >119 && totalTime <= 179){
leftOverMinutes = totalTime - 120;
hoursLabel.text = [NSString stringWithFormat:@"2"];
hourDecimalNumber = 2;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >179 && totalTime <= 239){
leftOverMinutes = totalTime - 180;
hoursLabel.text = [NSString stringWithFormat:@"3"];
hourDecimalNumber = 3;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >239 && totalTime <= 299){
leftOverMinutes = totalTime - 240;
hoursLabel.text = [NSString stringWithFormat:@"4"];
hourDecimalNumber = 4;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >299 && totalTime <= 359){
leftOverMinutes = totalTime - 300;
hoursLabel.text = [NSString stringWithFormat:@"5"];
hourDecimalNumber = 5;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >359 && totalTime <= 419){
leftOverMinutes = totalTime - 360;
hoursLabel.text = [NSString stringWithFormat:@"6"];
hourDecimalNumber = 6;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >419 && totalTime <= 479){
leftOverMinutes = totalTime - 420;
hoursLabel.text = [NSString stringWithFormat:@"7"];
hourDecimalNumber = 7;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >479 && totalTime <= 539){
leftOverMinutes = totalTime - 480;
hoursLabel.text = [NSString stringWithFormat:@"8"];
hourDecimalNumber = 8;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >539 && totalTime <= 599){
leftOverMinutes = totalTime - 540;
hoursLabel.text = [NSString stringWithFormat:@"9"];
hourDecimalNumber = 9;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >599 && totalTime <= 659){
leftOverMinutes = totalTime - 600;
hoursLabel.text = [NSString stringWithFormat:@"10"];
hourDecimalNumber = 10;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >659 && totalTime <= 719){
leftOverMinutes = totalTime - 660;
hoursLabel.text = [NSString stringWithFormat:@"11"];
hourDecimalNumber = 11;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >719 && totalTime <= 779){
leftOverMinutes = totalTime - 720;
hoursLabel.text = [NSString stringWithFormat:@"12"];
hourDecimalNumber = 12;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >779 && totalTime <= 839){
leftOverMinutes = totalTime - 780;
hoursLabel.text = [NSString stringWithFormat:@"13"];
hourDecimalNumber = 13;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >839 && totalTime <= 899){
leftOverMinutes = totalTime - 840;
hoursLabel.text = [NSString stringWithFormat:@"14"];
hourDecimalNumber = 14;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >899 && totalTime <= 959){
leftOverMinutes = totalTime - 900;
hoursLabel.text = [NSString stringWithFormat:@"15"];
hourDecimalNumber = 15;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >959 && totalTime <= 1019){
leftOverMinutes = totalTime - 960;
hoursLabel.text = [NSString stringWithFormat:@"16"];
hourDecimalNumber = 16;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1019 && totalTime <= 1079){
leftOverMinutes = totalTime - 1020;
hoursLabel.text = [NSString stringWithFormat:@"17"];
hourDecimalNumber = 17;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1079 && totalTime <= 1139){
leftOverMinutes = totalTime - 1080;
hoursLabel.text = [NSString stringWithFormat:@"18"];
hourDecimalNumber = 18;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1139 && totalTime <= 1199){
leftOverMinutes = totalTime - 1140;
hoursLabel.text = [NSString stringWithFormat:@"19"];
hourDecimalNumber = 19;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1199 && totalTime <= 1259){
leftOverMinutes = totalTime - 1200;
hoursLabel.text = [NSString stringWithFormat:@"20"];
hourDecimalNumber = 20;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1259 && totalTime <= 1319){
leftOverMinutes = totalTime - 1260;
hoursLabel.text = [NSString stringWithFormat:@"21"];
hourDecimalNumber = 21;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1319 && totalTime <= 1379){
leftOverMinutes = totalTime - 1320;
hoursLabel.text = [NSString stringWithFormat:@"22"];
hourDecimalNumber = 22;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1379 && totalTime <= 1439){
leftOverMinutes = totalTime - 1380;
hoursLabel.text = [NSString stringWithFormat:@"23"];
hourDecimalNumber = 23;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1439 && totalTime <= 1499){
leftOverMinutes = totalTime - 1440;
hoursLabel.text = [NSString stringWithFormat:@"24"];
hourDecimalNumber = 24;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1499 && totalTime <= 1559){
leftOverMinutes = totalTime - 1500;
hoursLabel.text = [NSString stringWithFormat:@"25"];
hourDecimalNumber = 25;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1559 && totalTime <= 1619){
leftOverMinutes = totalTime - 1560;
hoursLabel.text = [NSString stringWithFormat:@"26"];
hourDecimalNumber = 26;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1619 && totalTime <= 1679){
leftOverMinutes = totalTime - 1620;
hoursLabel.text = [NSString stringWithFormat:@"27"];
hourDecimalNumber = 27;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1679 && totalTime <= 1739){
leftOverMinutes = totalTime - 1680;
hoursLabel.text = [NSString stringWithFormat:@"28"];
hourDecimalNumber = 28;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1739 && totalTime <= 1799){
leftOverMinutes = totalTime - 1740;
hoursLabel.text = [NSString stringWithFormat:@"29"];
hourDecimalNumber = 29;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1799 && totalTime <= 1859){
leftOverMinutes = totalTime - 1800;
hoursLabel.text = [NSString stringWithFormat:@"30"];
hourDecimalNumber = 30;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
errorLabel.hidden = TRUE;
}
else if (totalTime >1859){
hoursLabel.text = [NSString stringWithFormat:@"Error"];
hoursDecimalLabel.text = [NSString stringWithFormat:@"Error"];
errorLabel.hidden = FALSE;
}
//Minutes Label
if (leftOverMinutes < 10) {
minutesLabel.text = [NSString stringWithFormat:@"0%d", leftOverMinutes];
}
else
minutesLabel.text = [NSString stringWithFormat:@"%d", leftOverMinutes];
//Minutes Decimal Label
if (leftOverMinutes >=0 && leftOverMinutes <=2) {
minutesDecimalLabel.text = [NSString stringWithFormat:@"0"];
}
else if (leftOverMinutes >=3 && leftOverMinutes <=8){
minutesDecimalLabel.text = [NSString stringWithFormat:@"1"];
}
else if (leftOverMinutes >=9 && leftOverMinutes <=14){
minutesDecimalLabel.text = [NSString stringWithFormat:@"2"];
}
else if (leftOverMinutes >=15 && leftOverMinutes <=20){
minutesDecimalLabel.text = [NSString stringWithFormat:@"3"];
}
else if (leftOverMinutes >=21 && leftOverMinutes <=26){
minutesDecimalLabel.text = [NSString stringWithFormat:@"4"];
}
else if (leftOverMinutes >=27 && leftOverMinutes <=32){
minutesDecimalLabel.text = [NSString stringWithFormat:@"5"];
}
else if (leftOverMinutes >=33 && leftOverMinutes <=38){
minutesDecimalLabel.text = [NSString stringWithFormat:@"6"];
}
else if (leftOverMinutes >=39 && leftOverMinutes <=44){
minutesDecimalLabel.text = [NSString stringWithFormat:@"7"];
}
else if (leftOverMinutes >=45 && leftOverMinutes <=50){
minutesDecimalLabel.text = [NSString stringWithFormat:@"8"];
}
else if (leftOverMinutes >=51 && leftOverMinutes <=56){
minutesDecimalLabel.text = [NSString stringWithFormat:@"9"];
}
else if (leftOverMinutes >=57 && leftOverMinutes <=60){
minutesDecimalLabel.text = [NSString stringWithFormat:@"0"];
hourDecimalNumber = hourDecimalNumber + 1;
hoursDecimalLabel.text = [NSString stringWithFormat:@"%i", hourDecimalNumber];
}
}
@end
© Stack Overflow or respective owner