xcode 存储到的对象的潜在泄漏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18088271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Potential leak of an object stored into
提问by Jesús Ayala
I'm analyzing this block of code from an SDK and basing the kind of error on the answer of my latest question:
我正在分析来自 SDK 的这段代码,并根据我最新问题的答案确定错误类型:
如何在 iOS 中正确释放内存:内存永远不会被释放;指向的潜在内存泄漏
dasblinkenlight suggested me to create an NSData object for could releasing my uint8_t *bytes...
dasblinkenlight 建议我创建一个 NSData 对象,用于释放我的 uint8_t *bytes ...
But here in this code:
但在这段代码中:
/**
* 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();
}
I got the same kind of error on these lines:
我在这些行上遇到了同样的错误:
potential leak of an object stored into 'myColorspace'
存储到“myColorspace”中的对象的潜在泄漏
CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);
potential leak of an object stored into 'myGradient'
存储到“myGradient”中的对象的潜在泄漏
UIGraphicsPopContext();
I tried with:
我试过:
free(myColorspace);
free(myGradient);
but I keep the same kind of issues, what can I do to resolve it
但我一直有同样的问题,我能做些什么来解决它
thanks in advance all your support
预先感谢您的所有支持
回答by Justin Meiners
Listen to exactly what the errors are telling you.
听听错误告诉你什么。
"potential leak of an object stored into myColorspace
"
“存储到的对象的潜在泄漏myColorspace
”
Lets look at colorspace and see if we can find the problem. myColorspace
is created CGColorSpaceCreateDeviceRGB
so retain count is +1, but then never released. This is unbalanced and it needs to be released at the end. We need to add a CGColorSpaceRelease(myColorSpace);
让我们看看色彩空间,看看我们是否能找到问题。myColorspace
被创建,CGColorSpaceCreateDeviceRGB
因此保留计数为 +1,但从未被释放。这是不平衡的,需要最后释放。我们需要添加一个CGColorSpaceRelease(myColorSpace);
"potential leak of an object stored into myGradient
"
“存储到的对象的潜在泄漏myGradient
”
Same problem, creation with retain count +1, with no corresponding release. Add a CGGradientRelease(myGradient);
同样的问题,创建时保留计数 +1,没有相应的发布。添加一个CGGradientRelease(myGradient);
Do not use free
on anything created with a framework Create
function. The internal structure could be more complex, and free will not properly take care of all the memory. Use a corresponding Release
function.
不要free
在用框架Create
函数创建的任何东西上使用。内部结构可能更复杂,free 不会妥善处理所有内存。使用相应的Release
功能。