ios - 为什么会有 "potential leak"?
<p><p>Xcode 的分析器提示存在“潜在的对象泄漏”。以下方法中的第一行突出显示:</p>
<pre><code>- (void)retrieveBeginRestoreData {
self.restoreContext = ;
if (!self.restoreContext) self.restoreData = nil;
CGRect rect = {{0,0},self.image.size};
CGContextDrawImage(self.restoreContext, rect, self.image.CGImage);
self.restoreData = CGBitmapContextGetData(self.restoreContext);
}
</code></pre>
<p>我有一个这样声明的属性:</p>
<pre><code>@property (nonatomic, assign) CGContextRef restoreContext
</code></pre>
<p><code>newARGBBitmapContext</code> 由以下内容定义:</p>
<pre><code>- (CGContextRef)newARGBBitmapContext {
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
size_t bitmapByteCount;
size_t bitmapBytesPerRow;
// Get image width, height. We'll use the entire image.
size_t pixelsWide = CGImageGetWidth(self.CGImage);
size_t pixelsHigh = CGImageGetHeight(self.CGImage);
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
// Use the generic RGB color space.
// colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
return NULL;
}
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease( colorSpace );
return NULL;
}
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
free (bitmapData);
fprintf (stderr, "Context not created!");
}
// Make sure and release colorspace before returning
CGColorSpaceRelease( colorSpace );
return context;
}
</code></pre>
<p>我设法通过将 <code>restoreContext</code> 声明为头文件中的实例变量来解决此问题; “潜在泄漏”警告消失了。</p>
<p>问题:</p>
<ul>
<li>首先是什么问题?</li>
<li>当我停止将 <code>restoreContext</code> 声明为属性时,这个问题是如何解决的?</li>
<li>解决 <code>restoreContext</code> 被声明为属性的问题的正确方法是什么?</li>
</ul></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这一行</p>
<pre><code>self.restoreContext = ;
</code></pre>
<p>执行以下操作:</p>
<ol>
<li><p>它(可能)创建一个 <code>CGContext</code> 的实例对象。</p></li>
<li><p>由于方法名称以 <code>new</code> 开头,因此应用了所有权转移。这意味着接收者(您的代码)负责发布它。 </p></li>
</ol>
<p>当这行代码第二次运行时,已经存在的<code>CGContext</code>实例的引用被覆盖而不释放实例,它指向。较旧的实例会泄漏。 </p></p>
<p style="font-size: 20px;">关于ios - 为什么会有"potential leak"?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31110718/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31110718/
</a>
</p>
页:
[1]