I'm in .NET and using pointers to access raw image data. Currently I'm storing a reference to the Scan0 (and Stride) of the bitmap using:
BitmapData bmpData = bmp.LockBits(...);
*byte scan0 = (*byte)bmpData.Scan0;
Sometimes my program randomly crashes with errors like "Attempted to access protected memory". I'm assuming this is because my pointer becomes invalid after the .NET GC routinely compacts memory. So if I use IntPtr
instead of the *byte
pointer that I currently use, will the pointer remain valid after memory compaction?
BitmapData bmpData = bmp.LockBits(...);
IntPtr scan0 = bmpData.Scan0;
Obviously its near impossible to test this because I cannot replicate the "random" memory errors I'm getting, so I'm trying to arrive at a theoretically correct solution and hope it corrects my issues.
Edit: Please do not start a discussion on bitmap manipulation techniques. I just want to know if IntPtr
is any better than *byte
.