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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:49:24+00:00 2026-06-03T19:49:24+00:00

I have a quite strange problem: I am testing several function calls to a

  • 0

I have a quite strange problem:

I am testing several function calls to a unmanaged C dll with NUnit. The odd thing is, the test fails when it runs normally, but when i run it with the debugger (even with no break point) it passes fine.

So, has the debugger a wider memory access as the plain NUnit application?

i have isolated the call which fails. its passing back a char pointer to a string, which the marshaller should convert to a C# string. the C side looks like this:

#define get_symbol(a) ((a).a_w.w_symbol->s_name)
EXTERN char *atom_get_symbol(t_atom *a);

...

char *atom_get_symbol(t_atom *a) {
  return get_symbol(*a);
}

and the C# code:

[DllImport("csharp.dll", EntryPoint="atom_get_symbol")]
[return:MarshalAs(UnmanagedType.LPStr)]
private static extern string atom_get_symbol(IntPtr a);

the pointer which is returned from c is quite deep inside the code and part of a list. so do i just miss some security setting?

EDIT: here is the exception i get:

System.AccessViolationException : (translated to english:) there was an attempt to read or write protected memory. this might be an indication that other memory is corrupt.

at Microsoft.Win32.Win32Native.CoTaskMemFree(IntPtr ptr)
at ....atom_get_symbol(IntPtr a)

SOLUTION:

the problem was, that the marshaller wanted to free the memory which was part of a C struct. but it sould just make a copy of the string and leave the memory as is:

[DllImport("csharp.dll", EntryPoint="atom_get_symbol")]
private static extern IntPtr atom_get_symbol(IntPtr a);

and then in the code get a copy of the string with:

var string = Marshal.PtrToStringAnsi(atom_get_symbol(ptrToStruct));

great!

  • 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-03T19:49:27+00:00Added an answer on June 3, 2026 at 7:49 pm

    This will always cause a crash on Vista and up, how you avoided it at all isn’t very clear. The stack trace tells the tale, the pinvoke marshaller is trying to release the string buffer that was allocated for the string. It always uses CoTaskMemFree() to do so, the only reasonable guess at an allocator that might have been used to allocate the memory for the string. But that rarely works out well, C or C++ code almost always uses the CRT’s private heap. This doesn’t crash on XP, it has a much more forgiving memory manager. Which produces undiagnosable memory leaks.

    Notable is that the C declaration doesn’t give much promise that you can pinvoke the function, it doesn’t return a const char*. The only hope you have is to declare the return type as IntPtr instead of string so the pinvoke marshaller doesn’t try to release the pointed-to memory. You’ll need to use Marshal.PtrToStringAnsi() to convert the returned IntPtr to a string.

    You’ll need to test the heck out of it, call the function a billion times to ensure that you don’t leak memory. If that test crashes with an OutOfMemoryException then you have a big problem. The only alternative then is to write a wrapper in the C++/CLI language and make sure that it uses the exact same version of the CRT as the native code so that they both use the same heap. Which is tricky and impossible if you don’t have the source code. This function is just plain difficult to call from any language, including C. It should have been declared as int atom_get_symbol(t_atom* a, char* buf, size_t buflen) so it can be called with a buffer that’s allocated by the client code.

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

Sidebar

Related Questions

I have quite a strange problem with PHP and Apache on my local testing
I have a strange problem that I can't seem to solve. I've quite a
This is a quite strange problem. I have set a setting variable in Application
I am facing quite a strange problem at the moment, I have wordpress and
I have a very strange problem, which took already several hours of my attention:
I am facing quite strange problem. Application ~80.Mb Testing on simulator using Allocations Instrument,
Hallo, I have a quite strange problem in one of my C++ projects: I
I have got an strange problem but I am quite sure, for you it
I have quite a strange problem , While copying one WidgetCollection from one FlowPanel
i have a quite strange problem. my class has -among others- following memers: GLboolean

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.