I'm using locked bitmaps a lot recently, and I keep getting "attempted to access invalid memory" errors. This is mostly because the bitmap has been moved in memory. Some people using GCHandle.Alloc()
to allocate memory in the CLR and pin it. Does Bitmap.LockBits()
do the same? I don't understand the difference between "locking" memory and "pinning" memory. Can you also explain the terminology and the differences if any?
GCHandle.Alloc
is a more generic method, that allows you to allocate a handle to any managed object and pin it in memory (or not). Pinning memory prevents GC from moving it around, which is especially useful when you have to pass some data, for example an array, to a unmanaged code.
GCHandle.Alloc
will not help you access bitmap's data in any way, because pinning this object will just prevent the managed object from moving around (the Bitmap object) (and being garbage collected).
Bitmap however is a wrapper around native GDI+'s BITMAP
structure. It doesn't keep data in any managed array that you would have to pin, it just managed a native handle to GDI+ bitmap object. Because of that Bitmap.LockBits
is a way of telling this bitmap that you are interested in accessing it's memory, and it's just a wrapper around GdipBitmapLockBits
function. So your need of calling it has more to do with the fact that you are working with GDI+ bitmaps than with the fact, that you're working in managed environment with GC.
Once you have used LockBits
you should be able to access it's memory using pointers through BitmapData.Scan0
- it's an address of first byte of data. You should not have problems as long, as you do not access memory behind BitmapData.Scan0 + Height * Stride
.
And rememberto UnlockBits
when you are done.
In your case an attempted to access invalid memory
error is most probably caused by invalid memory allocation which you are doing in the unsafe part of code, e.g allocated array is smaller than number of pixels you are trying to put in that.
There is also no need to think about pinning the objects unless your image data is less than 85000 Bytes as only objects less than 85K will be moved in memory.
Another story would be if you pass the object to unmanaged code, for example in the c++ library for faster processing. In this case your exception is very possible if the passed image gets out of scope and will be garbage collected. In this case you can use GCHandle.Alloc(imageArray,GCHandleType.Pinned);
and than call Free if you do not need it any longer.
GCHandle.Alloc
on a pointer is no good either. –
Cormier BitmapData.Scan0
pointer gotten from Bitmap.LockBits()
. Bitmap
itself will fail if the data gets moved around, so there is never any reason for the LockBits()
caller to try to pin the data. And if the buffer is controlled by Bitmap
, which is the usual case, it doesn't get allocated from GC heap, so it is neither on the generational heap nor the large object heap. –
Cormier This is mostly because the bitmap has been moved in memory.
My answer was that it should not happen if data > 85k, but you've called it red herring. 2) And if the buffer is controlled by Bitmap, which is the usual case, it doesn't get allocated from GC heap
. What is GC heap? In C# array is an object and objects are always allocated on the heap, in case of c# Bitmap it's a managed heap. Maybe you think that OP allocates the c buffer and fills it by himself, than that would be just your assumption as he could open an existing bitmap from the c# –
Bertrando LockBits
gives you back an IntPtr
with the pixel data, not a byte[]
. –
Cormier GCHandle pinnedArray = GCHandle.Alloc(byteArray, GCHandleType.Pinned); IntPtr pointer = pinnedArray.AddrOfPinnedObject();
Does that mean that byteArray
is not managed any longer because I have a pointer to it's first byte? Everything stored on the heap has it's address so it's not a problem to get it. AFAIK .NET Bitmap class is completely managed. Otherwise .net dlls must be compiled unsafe. Do you have some proof for your words? –
Bertrando LockBits()
. "As far as I know .NET Bitmap class is completely unmanaged." You're guessing, you don't know, what you guessed is wrong, and that is why you are completely unqualified to answer this question. –
Cormier StringBuilder
does) or break type safety in Buffer.BlockCopy()
and pretty much the entire Marshal class. –
Cormier © 2022 - 2024 — McMap. All rights reserved.