Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:17:59+00:00 2026-05-31T07:17:59+00:00

I have a few spots in my code where I’m simply creating a new

  • 0

I have a few spots in my code where I’m simply creating a new Bitmap object, like so:

bmpTile = new Bitmap(8, 8, PixelFormat.Format32bppArgb);

Easy, right? For some reason, I randomly get exceptions on lines like this. By random, I mean that I can’t consistently reproduce the same exception on the same line after the same sequence of events. I can’t even create a unit test for you guys to look at.

The most common exception I get is:

ArgumentException: Parameter is not valid.
TargetSite: {Void .ctor(Int32, Int32, System.Drawing.Imaging.PixelFormat)}

The next most common one doesn’t even give me a stack trace from inside my program:

AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
StackTrace:
    at System.Drawing.SafeNativeMethods.Gdip.IntGdipDisposeImage(HandleRef image)
    at System.Drawing.SafeNativeMethods.Gdip.GdipDisposeImage(HandleRef image)
    at System.Drawing.Image.Dispose(Boolean disposing)
    at System.Drawing.Image.Finalize()
TargetSite: {Int32 IntGdipDisposeImage(System.Runtime.InteropServices.HandleRef)}

It looks like it’s having trouble disposing the old Bitmap whenever I try to create a new one, but why? I am using LockBits and UnlockBits ubiquitously, but I’m always doing them in pairs: I never call LockBits without a subsequent UnlockBits.

So what could cause Dispose() to fail like this?

EDIT: I should mention that it’s not happening on the second try. It happens on around the fifth or sixth try, meaning it has disposed of the first four or five Bitmaps without issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:18:00+00:00Added an answer on May 31, 2026 at 7:18 am

    Both problems have the same underlying cause. Your code after LockBits() is corrupting the unmanaged heap. It is typical to use pointers or Marshal.Copy() in such code so there’s no diagnostic when you write outside the bitmap boundaries. You only get an AccessViolation when you’re lucky. But more typically you won’t get them until later, when the heap allocator fails trying to allocate or release blocks from the heap. Very hard to diagnose since the crash location has nothing to with the original bug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few lines of code that start something like this: $trailheads =
I have few different applications among which I'd like to share a C# enum.
We have few components like libraries dlls When initially created I ran the following
Hi i am new to hibernate technology and i have few issues. Suppose if
I have a few specific places in my code where I use specific pixel
I am fairly new to MySQL with Java, but I have executed a few
I have quite a few lines of code that create objects and using various
I have few asynchronous tasks running and I need to wait until at least
I have few question in this regard When you create an internet page, does
I have few nested DIVs at page. I want to add event only for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.