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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:02:43+00:00 2026-06-15T13:02:43+00:00

I’m developing an application which involves native C++ to C# interop via a C++/CLR

  • 0

I’m developing an application which involves native C++ to C# interop via a C++/CLR wrapper.

I’m having trouble with the following operation, which causes a memory leak:

MyObject data = (MyObject)Marshal.PtrToStructure(ptr, typeof(MyObject));
Marshal.StructureToPtr(data, ptr, false);

(NOTE: I realise that i’m not actually doing anything to “data” at this time, so this is superfluous.)

Memory usage continues rising until the app crashes due to the system running out of memory. When I remove this code, this doesn’t happen. It’s not the garbage collector since a) it should collect when the system gets low on memory and b) i’ve tried forcing it with GC.Collect().

In fact, I’ve narrowed the leak down to the StructureToPtr command.

I’m unable to set the third parameter to “true”, since the memory was allocated by native C++, and C# considers this “protected” memory that cannot be freed.

I’ve checked that the populated Data struct is intact, has valid data, and is the same size as the equivalent native struct.

In my mind, this is what should be happening:

  1. My native structure referenced by ptr is marshalled and copied to the “data” managed structure

  2. The manage structure is copied back to the same memory referenced by ptr.

I can’t see how this can cause a memory leak, because it’s exactly the same size of structure, being copied back into the same memory space. But clearly it does, removing the code plugs the leak.

Is there some mechanic here which i’m not understanding correctly?

Edit: As requested, here are the declarations for “MyObject”.

C#:

[StructLayout(LayoutKind.Sequential)]
public struct MyObject
{
    [MarshalAs(UnmanagedType.I1)]
    public bool ParamOne;
    [MarshalAs(UnmanagedType.I1)]
    public bool ParamTwo;
    [MarshalAs(UnmanagedType.I1)]
    public bool ParamThree;
    [MarshalAs(UnmanagedType.I1)]
    public bool ParamFour;
    [MarshalAs(UnmanagedType.I1)]
    public bool ParamFive;
    [MarshalAs(UnmanagedType.I1)]
    public bool ParamSix;
    [MarshalAs(UnmanagedType.R4)]
    public float ParamSeven;
    [MarshalAs(UnmanagedType.R4)]
    public float ParamEight;
    [MarshalAs(UnmanagedType.R4)]
    public float ParamNine;
    public Vector2f ParamTen;
    public Vector2f ParamEleven;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ParamTwelve;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ParamThirteen;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ParamFourteen;
    public IntPtr ParamFifteen;
    public IntPtr ParamSixteen;
}

C++:

struct MyObject
{
    public:
    bool ParamOne;
    bool ParamTwo;      
    bool ParamThree;
    bool ParamFour;
    bool ParamFive;
    bool ParamSix;
    float ParamSeven;
    float ParamEight;
    float ParamNine;
    Vector2f ParamTen;
    Vector2f ParamEleven;
    wchar_t * ParamTwelve;
    wchar_t * ParamThirteen;
    wchar_t * ParamFourteen;
    void * ParamFifteen; 
    void * ParamSixteen;
};

The definition of Vector2f is as follows:

[StructLayout(LayoutKind.Sequential)]
public struct Vector2f
{
    [MarshalAs(UnmanagedType.R4)]
    float x;
    [MarshalAs(UnmanagedType.R4)]
    float y;
}
  • 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-15T13:02:44+00:00Added an answer on June 15, 2026 at 1:02 pm

    You have pointers to strings in your structure. Those pointers are assigned in your unmanaged code (using new wchar_t[<a number>]), right? When marshaling those pointers to managed code, marshaler creates managed string and fills them with contents of the unmanaged character arrays. When marshaling them back to unmanaged code, the marshaler copies the whole struct content, including the character pointers, assigning them new values (allocating memory for each string using CoTaskMemAlloc()). That’s what the third parameter of the Marshal.StructureToPtr is for. If it is set to true, the marshaler tries to deallocates the memory pointed by character pointers (using CoTaskMemFree()). If you have allocated the memory for character pointers using new operator, you cannot set that parameter to true, and by marshaling back to unmanaged, you lose the pointers to allocated memories (marshaller overwrites them with new values). And since you are not deallocating the memory allocated by marshaler, you end up with memory leak.

    The best option to deal with this situation is this:

    Marshal strings as pointers and use Marshal.PtrToStringUni() to convert them to strings:

    [StructLayout(LayoutKind.Sequential)]
    public struct MyObject
    {
        [MarshalAs(UnmanagedType.I1)]
        public bool ParamOne;
        [MarshalAs(UnmanagedType.I1)]
        public bool ParamTwo;
        [MarshalAs(UnmanagedType.I1)]
        public bool ParamThree;
        [MarshalAs(UnmanagedType.I1)]
        public bool ParamFour;
        [MarshalAs(UnmanagedType.I1)]
        public bool ParamFive;
        [MarshalAs(UnmanagedType.I1)]
        public bool ParamSix;
        [MarshalAs(UnmanagedType.R4)]
        public float ParamSeven;
        [MarshalAs(UnmanagedType.R4)]
        public float ParamEight;
        [MarshalAs(UnmanagedType.R4)]
        public float ParamNine;
        public Vector2f ParamTen;
        public Vector2f ParamEleven;
        public IntPtr ParamTwelve;    // <-- These are your strings
        public IntPtr ParamThirteen;  // <--
        public IntPtr ParamFourteen;  // <--
        public IntPtr ParamFifteen;
        public IntPtr ParamSixteen;
    }
    

    and the marshaling:

        MyObject data = (MyObject)Marshal.PtrToStructure(ptr, typeof(MyObject));
        var str1 = Marshal.PtrToStringUni(data.ParamTwelve);
        var str2 = Marshal.PtrToStringUni(data.ParamThirteen);
        var str3 = Marshal.PtrToStringUni(data.ParamFourteen);
        Marshal.StructureToPtr(data, ptr, false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.