質問

私はこのコードを書いた:

 bitmapData = calloc(1, bitmapByteCount );
 context = CGBitmapContextCreate (bitmapData,
                                 pixelsWide,
                                 pixelsHigh,
                                 8,
                                 bitmapBytesPerRow,
                                 colorSpace,
                                 kCGImageAlphaOnly);

私がこれを行うとき、CGBitmapContextは私のbitmapDataをコピーしているので、これらの行の後に私は書くべきです

free(bitmapData); 
役に立ちましたか?

解決

必要とすれば bitmapData それを解放しないでください。あなたがそれを必要としないならば、合格してください NULL 代わりにパラメータとして、Quartzはメモリ自体を割り当てます(iOS4.0以降)。

データ:描画がレンダリングされるメモリ内の宛先へのポインタ。このメモリブロックのサイズは、少なくとも(bytesPerRow*height)バイトである必要があります。IOS4.0以降およびMac OS X v10.6以降では、Quartzでビットマップにメモリを割り当てたい場合はNULLを渡すことができます。これにより、独自のメモリ管理から解放され、メモリリークの問題が軽減されます。

しかし、クォーツはコピーしません bitmapData, 、それはそこにレンダリングを行います。あなたが解放した後 context そのメモリを解放する必要があります。

編集:アップルのサンプルの一つで プロジェクト, 、メモリは解放されますが、すぐには解放されません:

float drawStage3(CGContextRef context, CGRect rect)
{
    // ...
    cachedData = malloc( (((ScaledToWidth * 32) + 7) / 8) * ScaledToHeight);
    // ...
    bitmapContext = CGBitmapContextCreate(cachedData /* data */,
    // ...
    CFRelease(bitmapContext);
    // ...
    // Clean up
    CFRelease(cachedImage);
    free(cachedData);
}

他のヒント

EDIT:

Your code is allocating a block of memory via calloc- you own that block of memory. So, you own freeing that memory. The CGBitmapContext create is just creating a context using the block of memory that you created (which is why you have to pass it in). When you are done with that block of memory you should free it.

I would do CFRelease on the context first. Whatever resources the context creates will be taken care of by the CFRelease.

The "Create Rule" in the Core Foundation Memory Guide Says:

Core Foundation functions have names that indicate when you own a returned object:

Object-creation functions that have “Create” embedded in the name; Object-duplication functions that have “Copy” embedded in the name. If you own an object, it is your responsibility to relinquish ownership (using CFRelease) when you have finished with it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top