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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:38:17+00:00 2026-05-12T17:38:17+00:00

I have a DLL, which is designed in C++, included in a C# project

  • 0

I have a DLL, which is designed in C++, included in a C# project and I have strange AccessViolationExceptions happening unrationally. I suspect that my garbage isn’t collected correctly. I have an unmanaged method apiGetSettings (from the DLL) which should copy data to a Settings object (actually a struct in the original code, but .NET InterOp only allowed importing the data as class objects. I use the System.Runtime.InteropServices.Marshal methods to allocate and deallocate memory, but it might leave garbage behind that crashes everything.

Now, should I implement IDisposable methods in the Settings class (is it unmanaged?). If so, how do I dispose of the strings marshalled as UnmanagedType.ByValTStr and how do I dispose of the Settings objects?

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
class Settings
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
internal string d;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
internal string t;
internal int b;
}

[DllImport(".\\foobar.dll", EntryPoint = "getSettings")]
private static extern int apiGetSettings(IntPtr pointerToSettings);

void GetSettings(ref Settings settings)
{
int debug = 0;

// Initialize a pointer for the structure and allocate memory
IntPtr pointerToSettings = Marshal.AllocHGlobal(43);

// Connect the pointer to the structure
Marshal.StructureToPtr(settings, pointerToSettings, true);

// Point the pointer
debug = apiGetSettings(pointerToSettings);

// Copy the pointed data to the structure
Marshal.PtrToStructure(pointerToSettings, settings);

// Free the allocated memory
Marshal.FreeHGlobal(pointerToSettings);
}
  • 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-12T17:38:18+00:00Added an answer on May 12, 2026 at 5:38 pm

    No, you don’t need to implement IDisposable. Your Settings class is a managed class (those attributes are just for runtime purposes) and will be garbage collected.

    My first guess: you’re allocating 43 bytes, but both your strings add up to over 70 bytes (remember, unless you’re on ancient Win98/Me, the size of one character is 2 bytes), so you’re not allocating enough. Use Marshal.SizeOf instead to dynamically detemine the size of the structure.

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

Sidebar

Related Questions

I have a C++ dll which implements several COM interfaces, that I'm trying to
my project is written in c#. I have a usercontrol that I've designed. In
I have written a DLL which exports a function that creates a window using
We have a DLL project which has existed for a long time (maybe as
I have a DLL which contains a class with static members . I use
I have a DLL which provided a decoding function, as follows: function MyDecode (Source:
I have a dll which I disassemble in Reflector, I then generate a class.
We have inherited VB6 dll which we need to make changes to. We have
I have a .NET dll which needs to read it's config settings from it's
I have a native DLL which outputs via stdout / stderr. I have a

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.