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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:49:38+00:00 2026-05-19T01:49:38+00:00

I have created a C++/CLI mixed DLL which I am using from C# Winforms

  • 0

I have created a C++/CLI mixed DLL which I am using from C# Winforms application.
I have carefuly checked Build config to be sure that I am linking to debug libs in Debug mode and non-debug libs in Release.

For now the application is doing nothing, just creating a native class in a managed wrapper like this (singleton pattern to ensure a single instance of the class):

static ManagedClassWrapper ^ GetInstance(){
                if(_me == nullptr){
                    _me = gcnew ManagedClassWrapper();
                    _me->_Impl = new NativeClass();
                }

                return _me;
            };

where _me and _impl is

private:
    NativeClass * _Impl;
    static ManagedClassWrapper ^ _me = nullptr;

In the form on a button click I do just this:

private void button1_Click(object sender, EventArgs e)
{
    ManagedClassWrapper mcw = ManagedClassWrapper.GetInstance();
}

Also I have a standard native entry point as usual DllMain. In the DEBUG build I use

_CrtSetReportHook( QaDMemManager::report );
_CrtSetDbgFlag((_CRTDBG_LEAK_CHECK_DF) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));

at the beginning of DllMain, an in DEBUG build I also hav redefined new:

#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#define LOG_LEVEL Logger::NOTICE
#include <stdlib.h>
#include <crtdbg.h>
#pragma warning(disable:4291)
#define new new(_NORMAL_BLOCK,__FILE__, __LINE__)
#else
#define LOG_LEVEL Logger::INFO
#endif

as I usually do for my non-MFC apps to get a nice memory leaks.

The constructor of NativeClass is empty.

Everything works fine in Debug builds, I see memory leaks in native code, no crashes.

But in Release build one time out of 10 my app just crashes when I click that button1.
It means: I can launch 10 instances of my app, 9 will work ok no matter how many times I click the button1, but the 10th will crash every time I click the button1 (after the crash I click Continue in the exception window and so I can click button1 many times).

The exception is the following:

************** Exception Text **************
System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at _initterm((fnptr)* pfbegin, (fnptr)* pfend)
   at <CrtImplementationDetails>.LanguageSupport.InitializeNative(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   at .cctor()
   --- End of inner exception stack trace ---
   at TestAudioInOut.TestForm.button1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
TestAudioInOut
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///V:/Test/bin/Release/Test.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
Mixed.DLL
    Assembly Version: 1.0.4026.39493
    Win32 Version: 
    CodeBase: file:///V:/Test/bin/Release/Mixed.DLL
----------------------------------------

What could be the problem (as I understand, TypeInitializationException means there is something wrong with construction of objects) and why it is only in Release mode?

  • 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-19T01:49:38+00:00Added an answer on May 19, 2026 at 1:49 am

    It doesn’t have anything to do with the code snippet you posted, the code bombs before ManagedClassWrapper gets created. The <Module> class is a wrapper class around all of the non-ref class code that you wrote. It bombs when it tries to call the initializers of your unmanaged code. It’s an AccessViolation, the usual way for unmanaged code to take a nose-dive.

    To debug this you’ll have to enable unmanaged debugging in your C# project. Project + Properties, Debug tab, tick “Enable unmanaged code debugging”. Then Debug + Exceptions, tick the Thrown flag on “Win32 Exceptions”. Run your code until the crash happens, the debugger will stop at the crash location. Should give you some idea where the bug is located. Use the normal debugging techniques that you’re familiar with when working with unmanaged code. Good luck with it.

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

Sidebar

Related Questions

I have created an android application that calls (using kSOAP library) a SOAP based
I have a native regular C++ Dll which I want to call from C#
I have a CLI application, which is digging some data, in case of need,
I have created a C++ CLI wrapper for native C++ code, which in turn
I have dll which accepts Pointer to array of bytes from C++ and try
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created a JSP / servlets application running in Tomcat 7. It runs
I have this query which when run in mysql cli gives the proper result
I have a java.sql.Clob object which I populate from an Oracle query and then
In some C++/CLI code I have a native class which has a factory method

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.