存储在对象中的潜在的泄漏
我正在分析这个来自SDK的代码块,并根据我最近的问题的答案,
如何在iOS中正确释放内存:内存永远不会释放; 潜在的内存泄漏
dasblinkenlightbuild议我创build一个NSData对象可以释放我的uint8_t *字节…
但是在这个代码中:
/** * this will set the brush texture for this view * by generating a default UIImage. the image is a * 20px radius circle with a feathered edge */ -(void) createDefaultBrushTexture{ UIGraphicsBeginImageContext(CGSizeMake(64, 64)); CGContextRef defBrushTextureContext = UIGraphicsGetCurrentContext(); UIGraphicsPushContext(defBrushTextureContext); size_t num_locations = 3; CGFloat locations[3] = { 0.0, 0.8, 1.0 }; CGFloat components[12] = { 1.0,1.0,1.0, 1.0, 1.0,1.0,1.0, 1.0, 1.0,1.0,1.0, 0.0 }; CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB(); CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations); CGPoint myCentrePoint = CGPointMake(32, 32); float myRadius = 20; CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint, 0, myCentrePoint, myRadius, kCGGradientDrawsAfterEndLocation); UIGraphicsPopContext(); [self setBrushTexture:UIGraphicsGetImageFromCurrentImageContext()]; UIGraphicsEndImageContext(); }
我在这些行上遇到了同样的错误:
存储在“myColorspace”中的对象的潜在泄漏
CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);
存储在“myGradient”中的对象的潜在泄漏
UIGraphicsPopContext();
我试着用:
free(myColorspace); free(myGradient);
但我保持同样的问题,我可以做些什么来解决它
在此先感谢您的支持
仔细听听错误告诉你什么。
“存储在myColorspace
中的对象的潜在泄漏
好吧,让我们看看色彩空间,看看有什么问题。 创buildmyColorspace
CGColorSpaceCreateDeviceRGB
保留count +1,然后永远不会释放。 这是不平衡的,最后需要发布。 添加一个CGColorSpaceRelease(myColorSpace);
“存储到myGradient
中的对象的潜在泄漏”
同样的问题,保留数为+1的创build,没有相应的版本。 添加一个CGGradientRelease(myGradient);
不要在使用框架Create
函数Create
任何东西上使用free
。 内部结构可能更复杂,而且免费不会照顾内存。 使用相应的Release
function。