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

  • SEARCH
  • Home
  • 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 8790067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:33:25+00:00 2026-06-13T22:33:25+00:00

I am writing some image processing code and use C# to do low level

  • 0

I am writing some image processing code and use C# to do low level pixel manipulation. Every once in a while, an accessViolationException happens.

There are several approaches to this typical problem, some think code should be written robustly so to not have access violation exceptions, and as far as I try, the application is going fine however I would like to add a try catch so that iff something were to happen, the application would not fail in too ugly of a way.

So far, I have put in some example code to test it out

unsafe
{
    byte* imageIn = (byte*)img.ImageData.ToPointer();
    int inWidthStep = img.WidthStep;
    int height = img.Height;
    int width = img.Width;
    imageIn[height * inWidthStep + width * 1000] = 100; // make it go wrong
}

When I put a try catch around this statement, I still get an exception. Is there a way to catch exceptions generated in an unsafe block?

Edit: as stated down below, this type of exception is no longer handled unless the checking of them is explicitly enabled by adding this attribute to the function and adding the “using System.Runtime.ExceptionServices”.

[HandleProcessCorruptedStateExceptions]
    public void makeItCrash(IplImage img)
    {
        try
        {
            unsafe
            {
                byte* imageIn = (byte*)img.ImageData.ToPointer();
                int inWidthStep = img.WidthStep;
                int height = img.Height;
                int width = img.Width;
                imageIn[height * inWidthStep + width * 1000] = 100; // to make it crash
            }
        }
        catch(AccessViolationException e)
        {
            // log the problem and get out
        }
    }
  • 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-06-13T22:33:26+00:00Added an answer on June 13, 2026 at 10:33 pm

    Check the sizes and return an ArgumentOutOfRangeException if the parameters make you write outside of the image.

    An AccessViolationException is a Corrupted State Exception (CSE), not a Structured Exception Handling (SEH) exception. Starting with .NET 4, catch(Exception e) won’t catch CSE’s unless you specify it with an attribute. This is because you should write code that avoids CSE’s in the first place. You can read more about it here: http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035

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

Sidebar

Related Questions

I am writing a program to do some image processing. I am working with
While writing some C code, I decided to compile it to assembly and read
I've been writing an image processing algorithm now, and at some point I needed
I am writing a webserver which receives an image and do some processing and
I am writing some embedded code in C and need to use the rand()
I'm writing a program to do some image processing on the GPU. For this
In writing some image processing routines in Matlab, I found that I don't know
We are writing an image processing algorithm targeting some Intel hardware. Generally we prefer
I'm writing some code to acquire images that live on a server. The image
While writing some views to respond to ajax requests i find it somewhat strange

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.