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 8285669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:31:38+00:00 2026-06-08T11:31:38+00:00

This one is really starting to give me a headache :( I have an

  • 0

This one is really starting to give me a headache 🙁

I have an unmanaged DLL that I’m trying to interop with and it’s not going well. The application will sometimes work…but most times, randomly through an AccessViolationException and crash horribly.

I think I’ve narrowed it down to my mishandling of a single DllImport:

C++ function:

HTMLRENDERERDLL_REDIST_API void SetDataBuffer( int windowHandle, unsigned char* dataSource, int format, int stride, int totalBufferSize );

C# DllImport:

[DllImport("MyDll.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    static private extern unsafe void SetDataBuffer(Int32 windowHandle, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] dataSource, Int32 format, Int32 stride, Int32 totalBufferSize);

Call to said function:

var buffer = new byte[windowWidth * windowHeight * bytesPerPixel];
SetDataBuffer(windowHandle, buffer, (Int32)0, (Int32)(windowWidth * bytesPerPixel), (Int32)(windowWidth * windowHeight * bytesPerPixel));

Is there anything glaringly wrong with this? I suspect that dataSource is a culprit but…not sure how to prove it!

Thanks

  • 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-08T11:31:39+00:00Added an answer on June 8, 2026 at 11:31 am

    Your problem can be inferred from the name of the function. When you “set a buffer”, it is likely that the native code is going to use that buffer later. That’s incompatible with the garbage collector, it is going to move the array when it compacts the heap. That’s a big Kaboom when the native code then writes to the buffer, it is going to write to memory that isn’t there anymore. The most typical outcome is a FatalExecutionEngineException when the garbage collector detects that the heap integrity got compromised.

    The array needs to be pinned, something that the pinvoke marshaller does when it calls the function, but it unpins the array after the call.

    You can pin a managed array with GCHandle.Alloc(), but that’s pretty detrimental to the garbage collector if you leave it pinned for a long time. By far the best solution is to use Marshal.AllocHGlobal to allocate a chunk of unmanaged memory, it never moves.

    If you still have trouble then worry about the size of the buffer. And just plain misery in the native code, it rarely needs much help to fall over on an AccessViolation. That’s the standard failure mode for native code. Very hard to diagnose, impossible if you don’t have the source code for it. Contact the code owner for support, have a small repro snippet available to help him locate the problem.

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

Sidebar

Related Questions

this one is really easy. I'm trying to create a Regular Expression that will
This one I really think is not possible to solve through XSLT, so that
I really have problem with this one. So I have a jar with a
Really ripping my hair out on this one. I have a JAAS Authentication Provider
I am really having a trouble figuring this one out. I have a table
This is starting to really annoy me! I have an Excel sheet with a
Probably a really simple one this - I'm starting out with C# and need
This one is really making me pull my hair out. I have written a
Boy, this one is really weird. I expect the following code to print 1990,
Ok, this one is really weird... I can't show code for it exactly cause

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.