存储在对象中的潜在泄漏

Jes*_*ala 4 xcode memory-leaks ios

我正在从SDK分析这段代码,并根据我最新问题的答案出现错误:

如何在iOS中正确释放内存:永远不会释放内存; 潜在的内存泄漏指向

dasblinkenlight建议我创建一个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();
}
Run Code Online (Sandbox Code Playgroud)

我在这些行上遇到了同样的错误:

存储在'myColorspace'中的对象的潜在泄漏

CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);
Run Code Online (Sandbox Code Playgroud)

存储在'myGradient'中的对象的潜在泄漏

UIGraphicsPopContext();
Run Code Online (Sandbox Code Playgroud)

我尝试过:

free(myColorspace);
free(myGradient);
Run Code Online (Sandbox Code Playgroud)

但我保持同样的问题,我该怎么做才能解决它

在此先感谢您的支持

Jus*_*ers 14

仔细聆听错误告诉你的内容.

"存储在对象中的潜在泄漏myColorspace"

让我们看看色彩空间,看看我们是否能找到问题所在.myColorspace已创建,CGColorSpaceCreateDeviceRGB因此保留计数为+1,但从未发布.这是不平衡的,需要在最后发布.我们需要添加一个CGColorSpaceRelease(myColorSpace);

"存储在对象中的潜在泄漏myGradient"

同样的问题,使用保留计数+1创建,没有相应的释放.添加一个CGGradientRelease(myGradient);

不要free在使用框架Create函数创建的任何内容上使用.内部结构可能更复杂,而且free将无法妥善处理所有内存.使用相应的Release功能.

  • 只想添加名称中"Create"或"Copy"的任何Apple函数返回+1对象.请参阅http://developer.apple.com/library/ios/#DOCUMENTATION/CoreFoundation/Conceptual/CFMemoryMgmt/CFMemoryMgmt.html#//apple_ref/doc/uid/10000127i (2认同)