简体   繁体   中英

iphone beginner : Cannot figure out where the leak is

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];
}

尝试在标头中使用@property (nonatomic, retain) UILabel* firstMessage ,在实现中使用@synthesize firstMessage ,然后在dealloc方法中释放firstMessage对象。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM