【问题标题】:memory issue inside my touchesBegan methodh我的 touchesBegan 方法中的内存问题
【发布时间】:2013-10-11 06:02:51
【问题描述】:

我的项目中有内存问题。但我不知道如何解决它。这就是我正在做的事情。就像你在下面看到的那样,我有一张上面有城市的地图。当您点击一座城市时,这座城市就会亮起。

这就是我在 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

所以现在的问题。每件事都很顺利。但是在选择了几张图片后,应用程序关闭了,我没有收到任何错误消息。我已经为这个问题苦苦挣扎了好几个星期了。

有人可以帮我解决这个问题吗?

亲切的问候。

编辑

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

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.

【问题讨论】:

    标签: iphone ios objective-c uiview memory-leaks


    【解决方案1】:

    启用 NSZombieEnabled 以检查项目内的内存问题。

    1. 转到编辑方案 --> 参数 那么

    2- 选择“诊断”选项卡并点击“启用僵尸对象”

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

    一个典型的情况是当你过度释放一个对象并且你不知道是哪一个时:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      相关资源
      最近更新 更多