1

私は今、iPhoneアプリでメモリリークの問題に悩まされています。データを間違って読み取らなければならないように感じます。メモリを割り当てるたびに、オーバーヘッドが多すぎてリークが発生し、データを解放してもメモリ使用量がほとんど低下しないか、まったく低下しないようです。2 日間無駄にしたのは、フリップサイド ビュー コントローラーの UIWebview が URL をロードし、アプリのメモリ使用量が 3 mb から 7 に跳ね上がったことです。dealloc メソッドで webview を解放しましたが、メモリの巨大なブロックはまだ生きています。誰にも何か提案はありますか。

- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

nav_bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width+20,45)];
[self.view addSubview:nav_bar];
[UINavigationBar release];

rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
item = [[UINavigationItem alloc] initWithTitle:@"Flipside View"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[nav_bar pushNavigationItem:item animated:NO];
[rightButton release];
[item release];

NSAutoreleasePool *initPool = [[NSAutoreleasePool alloc] init];

web_view = [[UIWebView alloc]initWithFrame:CGRectMake(0,45,self.view.frame.size.width,self.view.frame.size.height - 45)];

web_view.autoresizesSubviews = YES;
web_view.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

NSString *urlAddress = @"http://www.tutorialpark.com/wpcontent/uploads/3/HeartBlending.jpg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[web_view loadRequest:requestObj];
[self.view addSubview:web_view];
[web_view release];
[initPool release];

[super viewDidLoad];
}

- (void)dealloc {
   [nav_bar removeFromSuperview];
   [web_view removeFromSuperview];
   [rightButton release];
   [super dealloc];
}

インデントについては申し訳ありませんが、私は今非常に腹を立てており、対処したくありません。

4

2 に答える 2

2

参照カウントがどのように機能するかについて混乱しているようです。

以下のコメントを参照してください。

- (void)viewDidLoad {
  self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

  UINavigationBar* nav_bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width+20,45)];
  // nav_bar retaincount 1
  [self.view addSubview:nav_bar];
  // nav_bar retaincount 2
  [nav_bar release];
  // nav_bar retaincount 1 - now controlled by self.view

  UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
  // rightButton retaincount 1

  UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"Flipside View"];
  // item retaincount 1

  item.rightBarButtonItem = rightButton;
  // rightButton retaincount 2

  [rightButton release];
  // rightButton retaincount 1 - now controlled by item

  item.hidesBackButton = YES;
  [nav_bar pushNavigationItem:item animated:NO];
  // item retaincount 2

  [item release];
  // item retaincount 1 - now controlled by nav_bar

  UIWebView* web_view = [[UIWebView alloc]initWithFrame:CGRectMake(0,45,self.view.frame.size.width,self.view.frame.size.height - 45)];
  // web_view retaincount 1

  web_view.autoresizesSubviews = YES;
  web_view.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

  NSString *urlAddress = @"http://www.tutorialpark.com/wpcontent/uploads/3/HeartBlending.jpg";

  NSURL *url = [NSURL URLWithString:urlAddress];
  // url is autoreleased, you can ignore..

  NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  // requestObj is autoreleased, you can ignore..

  [web_view loadRequest:requestObj];

  [self.view addSubview:web_view];
  // web_view retaincount 2

  [web_view release];
  // web_view retaincount 1 - now controlled by self.view

  [super viewDidLoad];
}

- (void)dealloc {
   // don't need to do anything because all the memory is controlled by self.view, will be released when the internal variable self.view is released.
   [super dealloc];
}
于 2011-08-25T03:05:08.613 に答える
1

[UINavigationBarリリース]; -これは何をしているのですか?[nav_barリリース]という意味ですか?-その場合は、後でnav_barに再度アクセスするコードの少し下で実行する必要があります。しかし、それはメンバー変数のようですか?したがって、deallocでリリースする必要があります。

rightButtonは2回リリースされています-1回はviewDidLoadに、もう1回はdeallocにあります。

自動リリースプールの目的を説明していただけますか?

于 2011-08-25T03:02:37.347 に答える