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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:03:03+00:00 2026-05-28T15:03:03+00:00

I write a module in C# that exports some functions to be used in

  • 0

I write a module in C# that exports some functions to be used in C.
I need to allocate some memory for some structs to be passed between C <-> C#.

What I allocate in C I do with malloc, and in C# I do with Marshal.AllocHGlobal() (to allocate unmanaged memory to be passed to C).

Is there any problem if I free() the memory allocated with Marshal.AllocHGlobal, and if I release memory with Marshal.FreeHGlobal() which was allocated with malloc?

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-05-28T15:03:03+00:00Added an answer on May 28, 2026 at 3:03 pm

    The golden rule is that you must deallocate from the same heap that was used to allocate the memory.

    If you allocate it with malloc(), you must deallocate it with the free() from the same C RTL. And likewise on the managed side, AllocHGlobal() should be balanced by FreeHGlobal().

    Now, AllocHGlobal() is implemented by calling the Win32 function LocalAlloc. So you could free such memory with a call to LocalFree on the native side. And vice versa.

    If you want to use a heap that is shared between native and managed, it is more common to use the COM heap. On the native side use CoTaskMemAlloc() and CoTaskMemFree(). On the managed side use Marshal.AllocCoTaskMem() and Marshal.FreeCoTaskMem().

    However, you should avoid designing the system like this. It is much simpler to stick to a rule that all memory allocated in the managed side is deallocated there, and likewise for the native side. If you don’t follow that rule you will likely soon lose track of who is responsible for what.

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

Sidebar

Related Questions

I need to write a module that gives me a page will all possible
I need to write a module that sends order data to an epayment service,
I need to write a kernel module that is not a device driver. That
I need to write some kernel module for my university classes and now I'm
Has anyone come across a Perl module that will parse (and write) kerberos configuration
I'm trying to write a unit test for a module that will give me
I would like to write a module that provides active record like functionality on
How to write a kernel module that creates a directory in /proc named mymod
I'm trying to write some Erlang that would filter an array in the form:
Basically, I want to write a kernel module that adds a possible filter to

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.