【问题标题】:Receiving Memory Warning Level =1, Level =2 And Then Crashes接收内存警告级别=1,级别=2,然后崩溃
【发布时间】:2010-10-14 13:11:46
【问题描述】:

我正在尝试开发 TilesGame,我正处于最后阶段。但我的项目开始出现意外行为。在随机时间它显示内存警告级别 = 1,级别 = 2,然后, 节目接收信号:“0”。 数据格式化程序暂时不可用,将在“继续”后重试。 (加载共享库“/Developer/usr/lib/libXcodeDebuggerSupport.dylib”时出现未知错误) 杀 退出
我不知道我在哪里做错了。我有大量的 ImageView,但它们被正确分配和释放。

关于 IBOutlets 有什么吗?我在我的 viewController 的 Dealloc 方法中发布了它们。

在 ViewController.h 中

 @property (nonatomic, retain) MyCustomButton *paveButton;
 @property (nonatomic, retain) MyCustomButton *slackenButton;
 @property (nonatomic, retain) MyCustomButton *tkeeperButton;
 @property (nonatomic, retain) MyCustomButton *spontButton;

 @property (nonatomic, retain) UIImageView *paveHammer;
 @property (nonatomic, retain) UIImageView *slackenHammer;
 @property (nonatomic, retain) UIImageView *tkeeperHammer;
 @property (nonatomic, retain) UIImageView *spontHammer;
 @property (nonatomic, retain) UIImageView *paveKnob;

在 ViewController.m 中 -

 @synthesize paveButton;
 @synthesize slackenButton;
@synthesize tkeeperButton;
@synthesize spontButton;
@synthesize paveHammer;
@synthesize slackenHammer;
@synthesize tkeeperHammer;
@synthesize spontHammer;
@synthesize paveKnob;



- (void)dealloc {
    [super dealloc];
[paveButton release];
[slackenButton release];
[tkeeperButton release];
[spontButton release];
[paveHammer release];
[slackenHammer release];
[tkeeperHammer release];
[spontHammer release];
[paveKnob release];

}

我在哪里做错了?有什么帮助吗?
提前致谢。

【问题讨论】:

  • 这不能回答你的问题,但 [super dealloc] 应该总是在你的 dealloc 方法结束时去:)

标签: iphone


【解决方案1】:

看起来您的应用程序正在被杀死,因为它使用了太多内存(尽管我猜您已经知道了!)

在你的 dealloc 方法中释放东西并不意味着它们会在内存不足时被释放。

您需要像这样发布任何 IBOutlets:

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
    [self setPaveButton:nil];
}

当内存不足警告发生时调用此方法。

只释放可以在 viewDidLoad 方法中重新创建的东西,以防你的视图被重新加载。

【讨论】:

  • 谢谢,我要试试这个。
  • 只想添加更多关于 [self setPaveButton:nil] 的信息;此行在属性方面将首先释放旧按钮,然后尝试保留新对象。这意味着该行将释放旧对象并保留零,这什么都不是。这样做是个好习惯。
  • 实际上,我的回答是完全错误的 :( 如果您收到内存不足警告但视图仍在显示,那么您最不想做的就是忘记您将按钮放在哪里!释放@987654322 @s 应该进入 viewDidUnload :(
【解决方案2】:

该应用正在尝试使用比可用内存更多的内存。使用 Instruments 检查泄漏和分配。也许尝试更少的瓷砖或更小的瓷砖。

【讨论】:

    【解决方案3】:

    您使用的图像尺寸很大,这就是导致错误“内存警告”的原因。请尝试使用尺寸较小且分辨率较低的图像。

    一切顺利。

    【讨论】:

      【解决方案4】:

      感谢所有回答我的问题的人,感谢您的建议。我已经修复了我的错误,我在代码中确实犯了一个愚蠢的错误,这给我带来了所有麻烦。

      我有大量的 ImageView,但它们被正确分配和释放。

      其实这是不对的。我有一个 ImageAnimation 并且我是这样做的-

      - (void) createBorderGlessEffect
      {
          borderImage.animationImages = [[NSArray alloc]initWithObjects:
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_1" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_2" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_3" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_4" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_5" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_6" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_7" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_8" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_9" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_10" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_11" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_12" ofType:@"png"]],
                                          [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BorderG_13" ofType:@"png"]],
                                          nil];
          [borderImage setAnimationDuration:0.6f];
          [borderImage setAnimationRepeatCount:1];
          [borderImage startAnimating];
      }
      

      这个方法在我的项目中被反复调用,看看我代码的粗体部分,我分配了一个 NsArray 的 13 个对象,这些对象从未被释放过。

      我现在正在这样做-

      borderImage.animationImages = [NSArray arrayWithObjects:...
      

      此答案实际上与我所质疑的内容无关,但我认为,我有责任在此提及我的解决方案。

      再次感谢。

      【讨论】:

      • 为什么每个单词都以大写字母开头?他们真的不需要!您应该将此答案标记为已接受(大勾号按钮),以便人们知道此问题已得到回答:)
      猜你喜欢
      • 2011-09-12
      • 2011-04-15
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      相关资源
      最近更新 更多