Tie destruction of an object (sealed) to destruction of an unmanaged buffer

Posted by testtestSO on Stack Overflow See other posts from Stack Overflow or by testtestSO
Published on 2010-12-25T10:35:10Z Indexed on 2010/12/25 10:53 UTC
Read the original article Hit count: 138

I'll explain my situation first: I'm interested of using the Bitmap constructor that takes scan0, stride and format, because I'm decoding tiled images and I'd like to choose my own stride so I can decode the tiles without caring about the bounds in the decoder part.

Anyway, the problem is that the documentation says: The caller is responsible for allocating and freeing the block of memory specified by the scan0 parameter. However, the memory should not be released until the related Bitmap is released.

I can't release the buffer easily, because the Bitmap is then passed to another class that will eventually destroy it and I don't have control over it. Is there some way (hacky, I know) to tell the GC to also release my buffer when the Bitmap is destroyed?

(Also, any alternative solution is welcome).

© Stack Overflow or respective owner

Related posts about c#

Related posts about garbage-collection