开发者

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 *)applicat开发者_JAVA百科ion {
    // 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];
}


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜