【问题标题】:App crash due to memory leak由于内存泄漏导致应用程序崩溃
【发布时间】:2012-05-23 11:52:53
【问题描述】:
- (void)viewDidLoad {

    webCollectionOnScroller=[[NSMutableArray alloc] init];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 370, 320, 94)];
    scroll.pagingEnabled = YES;
    currentWeb=0;
    globali=0;
    firstTime=0;
    [loadingWeb startAnimating];
    alertForLoading = [[UIAlertView alloc] initWithTitle:@"Loading..." message:@"link is being loaded.\n Please wait!" delegate:self cancelButtonTitle:@"Back" otherButtonTitles:nil];
    [alertForLoading show];
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    // Adjust the indicator so it is up a few pixels from the bottom of the alert
    indicator.center = CGPointMake(alertForLoading.bounds.size.width / 2, alertForLoading.bounds.size.height - 100);
    [indicator startAnimating];
    [alertForLoading addSubview:indicator];
    [NSThread detachNewThreadSelector:@selector(initializeParser)toTarget:self withObject:nil];
    [super viewDidLoad];
}

这是控制台错误“-[linksGallery respondsToSelector:]: message sent to deallocated instance 0x639a890 【切换到进程2211】 "

当我在主视图上评论发布声明时它不会崩溃

-(IBAction) goToLinks{
    linksGallery *showLinks=[[linksGallery alloc] initWithNibName:@"linksGallery" bundle:nil];
    [self.navigationController pushViewController:showLinks animated:YES];
        //[showLinks release];
}

【问题讨论】:

  • 请提供更多信息!我哪条线崩溃了? linksGallery 是什么?
  • 更改这一行并检查 --> indicator.center = CGPointMake(100, 200);可能是您失去了对 alertForLoading 的参考。如果它不起作用,请评论 NSThread 行并检查。我希望 initializeParser 方法有问题。
  • 崩溃不是由于“内存泄漏”,而是由于相反的原因——一个过早删除的对象。
  • 您在使用 ARC 吗?检查是否存在应该强的弱属性。
  • 类名应以大写字符开头。

标签: iphone ios


【解决方案1】:

先试试下面一行:

[super viewDidLoad];

在“dealloc()”函数内部:

[super dealloc];

在所有版本结束时。

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    该错误消息意味着 linksGallery 的实例在您发送消息“respondsToSelector”时已被释放。要调试它,请尝试在释放它时将其设置为 null,这样它就不会崩溃,尽管它可能也不会执行您想要的操作。

    【讨论】:

    • 这正是我所说的。您正在释放一个对象,然后尝试使用它。
    【解决方案3】:

    试试下面的代码

    -(IBAction) goToLinks{
        linksGallery *showLinks=[[linksGallery alloc] initWithNibName:@"linksGallery" bundle:nil];
        [self.navigationController pushViewController:[showLinks mutableCopy] animated:YES];
        [showLinks release];
    }
    

    -(IBAction) goToLinks{
        linksGallery *showLinks=[[[linksGallery alloc] initWithNibName:@"linksGallery" bundle:nil] autorelease];
        [self.navigationController pushViewController:showLinks animated:YES];
        //[showLinks release];
    }
    

    希望这会有所帮助

    【讨论】:

      【解决方案4】:

      让你的 ViewDidLoad 看起来像这样:

      - (void)viewDidLoad {
      
      [super viewDidLoad];
      
      webCollectionOnScroller=[[NSMutableArray alloc] init];
      scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 370, 320, 94)];
      scroll.pagingEnabled = YES;
      currentWeb=0;
      globali=0;
      firstTime=0;
      [loadingWeb startAnimating];
      alertForLoading = [[UIAlertView alloc] initWithTitle:@"Loading..." message:@"link is being loaded.\n Please wait!" delegate:self cancelButtonTitle:@"Back" otherButtonTitles:nil];
      [alertForLoading show];
      UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
      
      // Adjust the indicator so it is up a few pixels from the bottom of the alert
      indicator.center = CGPointMake(alertForLoading.bounds.size.width / 2, alertForLoading.bounds.size.height - 100);
      [indicator startAnimating];
      [alertForLoading addSubview:indicator];
      [NSThread detachNewThreadSelector:@selector(initializeParser)toTarget:self withObject:nil];
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-06-09
        • 2011-07-08
        • 2013-05-09
        • 1970-01-01
        • 2018-04-04
        • 2011-04-29
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多