2013-10-04 16 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實例當再次使用時。這是一種增加內存使用的調試輔助工具(沒有任何對象真的被釋放),但改善了錯誤報告。

一個典型的情況是當你過度釋放一個物體而你不知道哪一個: