2013-10-04 54 views
0

我的项目中有内存问题。但我不知道如何解决它。这就是我正在做的。就像你可以看到下面我有一个地图上的城市。当你点击一个城市时,城市亮起。我的touchesBegan方法中的内存问题

这就是我在touchesBegan方法中所做的。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
     NSLog(@"Touched"); 
     CGPoint c = [[touches anyObject] locationInView: self]; 
     struct CGPath *pat = (__bridge struct CGPath *)([arrayPaths objectAtIndex:0]); 
     struct CGPath *pat2 = (__bridge struct CGPath *)([arrayPaths objectAtIndex:1]); 
     // repeat this line 42 time (creating a struct for every city) 

     CGPathRef strokedPath = CGPathCreateCopy(pat); 
     CGPathRef strokedPath2 = CGPathCreateCopy(pat2); 
     //I also repeated this line 42 times 

     BOOL pointIsNearPath = CGPathContainsPoint(strokedPath, NULL, c, NO); 
     BOOL pointIsNearPath2 = CGPathContainsPoint(strokedPath2, NULL, c, NO); 
     //I also repeated this line 42 times 

     CFRelease(strokedPath); 
     CFRelease(strokedPath2); 
     //I also repeated this line 42 times 



if (pointIsNearPath){ 
     if([self.subviews containsObject:_imgLommel]) { 
      NSLog(@"Remove"); 
      [_imgLommel removeFromSuperview]; 
      [arrCities removeObject:[NSNumber numberWithInt:1]]; 
     }else{ 
      NSLog(@"add"); 
      [self addSubview:_imgLommel]; 
      [arrCities addObject:[NSNumber numberWithInt:1]]; 
     } 
    } 
    if (pointIsNearPath2){ 
     if([self.subviews containsObject:_imgHechtel]) { 
      NSLog(@"Remove"); 
      [_imgHechtel removeFromSuperview]; 
      [arrCities removeObject:[NSNumber numberWithInt:2]]; 
     }else{ 
      NSLog(@"add"); 
      [self addSubview:_imgHechtel]; 
      [arrCities addObject:[NSNumber numberWithInt:2]]; 
     } 
    } 
    //What I do here is I place an image with the colored city on top off the other images. If the image is already there I remove it. 

//I also repeated this line 42 times 

所以,现在的问题。每件事情都很好。但是选择几张图片后,应用程序关闭,我不会收到错误消息。我在这个问题上挣扎了好几个星期。

有人可以请这个帮我吗?

亲切的问候。

enter image description here

编辑

经过一些测试,我发现,当我注释掉以下行我没有得到错误:

if (pointIsNearPath43){ 
     if([self.subviews containsObject:_imgHoeselt]) { 
      NSLog(@"Remove"); 
      // [_imgHoeselt removeFromSuperview]; 
      [arrCities removeObject:[NSNumber numberWithInt:43]]; 
     }else{ 
      NSLog(@"add"); 
      // [self addSubview:_imgHoeselt]; 
      [arrCities addObject:[NSNumber numberWithInt:43]]; 
     } 
    } 

//Commented it also out in the other cities. 

回答

1

使NSZombieEnabled检查内存问题在项目内。

enter image description here

  1. 去编辑计划 - >参数 然后

enter image description here

2 - 选择 “诊断” 选项卡,然后单击 “启用僵尸对象”

这会将释放的对象转换为打印控制台警告的NSZombie实例当再次使用时。这是一种增加内存使用的调试辅助工具(没有任何对象真的被释放),但改善了错误报告。

一个典型的情况是当你过度释放一个物体而你不知道哪一个: