Ios 为什么CGPathRelease会导致内存泄漏?
当我试图释放一个路径时,它将导致大约5兆字节的内存泄漏。每次我调用CGreleasePath时,ram内存使用量将再次增加5兆字节。导致内存泄漏的原因可能是什么 这是使用“我的所有路径”的代码:Ios 为什么CGPathRelease会导致内存泄漏?,ios,objective-c,Ios,Objective C,当我试图释放一个路径时,它将导致大约5兆字节的内存泄漏。每次我调用CGreleasePath时,ram内存使用量将再次增加5兆字节。导致内存泄漏的原因可能是什么 这是使用“我的所有路径”的代码: //viewcontroller.h CGMutablePathRef path; //viewcontroller.m -(void)viewDidLoad{ path = CGPathCreateMutable; timer
//viewcontroller.h
CGMutablePathRef path;
//viewcontroller.m
-(void)viewDidLoad{
path = CGPathCreateMutable;
timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(movement1) userInfo:nil repeats:YES];
timer2 = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(motor) userInfo:nil repeats:YES];
}
-(void)movement1{
CGPathMoveToPoint(path, NULL, greentmporary.x, greentmporary.y);
CGPathAddLineToPoint(path, NULL, greenpoint1.x, greenpoint1.y);
}
-(void)motor{
CGPathRef thickpath = NULL;
thickpath = CGPathCreateCopyByStrokingPath(path, NULL, (6+bigsizeYellow), kCGLineCapSquare, kCGLineJoinMiter, 0);
if (CGPathContainsPoint(thickpath, NULL, greenpoint1, NO) && krockamedgreen == NO) {
NSLog(@"YES");
[self reset]
}
if (CGPathContainsPoint(thickpath, NULL, point1, NO) && krockamedgreen == NO) {
NSLog(@"YES");
[self reset]
}
if (thickpath) {
CFRelease(thickpath);
thickpath = NULL;
}
}
-(void)reset{
path = NULL;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//start again
{
path = CGPathCreateMutable;
}
我敢打赌你的问题在别处。您是否通过静态分析器(Xcode的“产品”菜单上的“分析”)运行代码?而且,当你说它“泄漏”时,这是由“仪器”中的“泄漏”工具报告的吗?还是只看“活字节”?告诉我们您如何知道
CGPathRef
实际上是问题的根源。如果您在确保静态分析器中没有警告后仍然存在问题,请使用代码示例更新您的问题。我现在已经更新了帖子。Okey发现了问题。我使用malloc为位图类型*void分配了字节,但我没有释放。运行xcode的怪异行为告诉我CGPATH有问题。您不需要在-reset
方法中释放path
,只需将其设置为NULL
。