1

I am trying to get my hands on some iphone development. To try to better understand things, I'm going first without IB.

I managed to build a basic app that displays some text. No big deal, until I ran it through Instruments. It shows me some leaks and I cannot understand them.

Any help on this would be greatly appreciated.

MyAppDelegate.h

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow            *window;
    MyViewController    *viewController;
}

@property (nonatomic, retain)  IBOutlet UIWindow *window;
@property (nonatomic, retain)  MyViewController *viewController;

@end

MyAppDelegate.m

@implementation MyAppDelegate

@synthesize window;
@synthesize viewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // NEXT LINE LEAKS   
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}
- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}
@end

MyViewController.h

@interface MyViewController : UIViewController {
    UILabel     *firstMessage;
}

MyViewController.m

-(void)loadView {

    // Background
    CGRect mainFrame = [[UIScreen mainScreen] applicationFrame];
    UIView *contentView = [[UIView alloc] initWithFrame:mainFrame];
    contentView.backgroundColor = [UIColor blackColor];
    self.view = contentView;
    [contentView release];

    // Add UILabel
    // NEXT LINE LEAKS   
    firstMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30)];
    firstMessage.font = [UIFont fontWithName:@"Helvetica" size:16];
    firstMessage.textColor = [UIColor whiteColor];
    firstMessage.backgroundColor = [UIColor blackColor]; 
    [self.view addSubview:firstMessage];
    [firstMessage release];
 }

-(void) viewDidLoad {
    NSString * msg;
    msg=[[NSString alloc] initWithFormat:@"stuff here"];
    firstMessage.text=msg;
    [msg release];
}

-(void)dealloc {
    [firstMessage release];
    [super dealloc];
}
5
  • what is in your viewcontrollers dealloc?
    – Stefan H
    Mar 6, 2011 at 23:09
  • there are no leaks in this code. Probably leaks in some other part of code that you didn't post. BTW, MyViewController *viewController; in declaration and @property (nonatomic, retain) BouncersViewController *viewController; in property. Correct this.
    – Max
    Mar 6, 2011 at 23:20
  • OK fixed this. I really commented out all other code, so it really doesn't do more. Nevertheless, I'll setup a new project with just what I wrote above and run it again through instruments. Looks silly, and I really shouldn't worry about it (it works), but I guess if I don't understand it, I have no chance to figure out real complex leaks.
    – Coriolan
    Mar 7, 2011 at 12:03
  • Added the dealloc method
    – Coriolan
    Mar 7, 2011 at 20:28
  • now you are over releasing your firstMessage, because you added the object to subview, your firstMessage will be released when the view is gone so you don't have to release it again in the dealloc
    – Seyther
    Mar 8, 2011 at 1:11

2 Answers 2

1

Try using @property (nonatomic, retain) UILabel* firstMessage in your header, @synthesize firstMessage in your implementation and then release your firstMessage object in the dealloc method.

0

Try not to release your firstMessage in loadView. Only release it in your controller's dealloc method.

5
  • then it definitely would be a leak in case of memory warning. There are no leaks in that code.
    – Max
    Mar 6, 2011 at 23:26
  • I'm concerned about your code because your controller releases firstMessage in loadView but then expects it to be present in viewDidLoad.
    – Dimitry
    Mar 6, 2011 at 23:27
  • So what? The retain count is 1. And when that label is removed from the superview it would be properly deallocated.
    – Max
    Mar 6, 2011 at 23:33
  • If it works, great. To me, it just looks like you're releasing prematurely. But I may be wrong.
    – Dimitry
    Mar 6, 2011 at 23:41
  • About Dimitri's comment : I agree, I felt uncomfortable releasing firstmessage in LoadView. But I kind of felt compelled to do so with all the examples I found on the web :-). Anyway, releasing it or not didn't seem to affect the leak detected by Instruments. But then, my real question is : is that the good way to manipulate my UILabels? I found other ways like tagging the UILabel when you create it, release it, then later, when you need it, retrieve it by its tag but it seems quite heavy...
    – Coriolan
    Mar 7, 2011 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.