Click here to Skip to main content
15,041,548 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
i am writting a program using Native C++ as dll , C++ CLI as a wrapper and C# for the UI. When I build it in Debug mode, it works fine , but in Release mode it throws an Access Exception. what's worse , when i debug it from C# code,i could not debug into the CLI code,much less the Native C++ DLL Code. But the exception throwed by the program showed that the error exists in the dll, i could not debug into it . Any help?

The exception may be produced like this:
In C# , i press button A, and then in Native C++ DLL , there is a class A uses malloc to allocate the memory and class B has an array of 256 chars and Class A will dynamic allocate the memory for creating a dynamic link list. The first time always works fine,after press button B,class A and Class B is deleted with out any problem. when i press Button A again.,The class A and Class B is created using new operator with out any problem, But when i access the char array in Class B, the program throws out the exception. If i commented dynamic link list code in class A , the exception gone. so i think the problem comes from the memory allocated . but , i could not debug into it.

Any help
jsolutions_uk 18-Apr-13 4:06am
I presume you have enabled native code debugging in the debug settings for the C# project? It's not enabled by default. Also the access violation sounds like either an initialisation problem somewhere (debug and release builds can behave slightly differently with uninitialised variables) or maybe you're accessing something via a pointer to memory that has been freed and you have not re-assigned the pointer value......

However, that is all very much speculation as we cannot see any of your code :)
Keanu L 18-Apr-13 11:16am
I'm sorry that the code belongs to a project ,so i need to keep secret......

I got the method from here.[^]
but i want to fix the exception , i am trying .
Make the Native C++ workspace as the startup project.
In Release mode, disable optimization from Project -> Properties -> Configuration Properties -> C/C++ -> Optimization.
Set the break point and press F5.
When it asks for the executable, browse to the EXE that loads this DLL.

you should use WinDbg

It might be easier to create a C++ application that directly uses the DLL and debug that application using the native debugger. It might be a console application that just call same code that would be called when you click on the buttons in C# application.

Have you enabled debug information in your release build?

You might also test options related to native debugging to see if it help.

Also, does the memory is all allocated and released from the native code. If not, the might be a conflict between native and mixed-mode (C++/CLI) DLL.

Is there any reason not to have native code in the C++/CLI DLL?

By the way debugging an application that have both C++ anc C# code is not as friendly as it should. Often the debugger has some problem inspecting some C++ code variables or is slow debugging such code.

I rarely have problems with the release version of my application so I do almost all debugging using the debug version.

Finally, you might enable some options to valiadate memory allocation in the DEBUG version to help you validate that the memory is correctly managed.

And for testing purpose, you might comment out some calls to delete to see if it fix the problem.

Finally, you have to remember to use the matching desallocation function when your release memory. Thus is the memory is allocated with new, it should be destroyed with delete. If it was allocated with new[], it should be destroyed with delete[] and same thing for any other allocation function (malloc, GlobalAlloc...). If you have custom allocator, you also have to be carefull to ensure that matching desallocator is used. And you also have to destroy object through a "compatible" pointer type (either the exact type or a base class type which has a virtual destructor).

And by the way. you have to ensure that your classes are always defined the same way if their definition dépends on the preprocessor or some pragma like aligment. You have to be particulary carefull if you specify different options for some of your source file as it can affect header that are somehow included by that file.

Also since you have 2 DLLs, not only you have to ensure that allocation and destruction are matching but that they come from the same DLL as the run-=time might be different.
Keanu L 18-Apr-13 11:12am
i had a dll which creates Thread using CreateThread and in this thread, there is class var declared in calling function( using operator new ) passed by point will allocate memory ( using malloc ) in this thread , after exit the thread, the calling function will clean up this class var including the memory allocated in thread , so i want to know is this operaton thread-safe.In some book <<Windows via C/C++>> said the C++/C Runtime librray does not allow two threads allocate the memory using malloc at the same time. i think in all my threads, the class var will allocate the memory but will not clean up and all the clean up operation is done by the calling function which breaks the heap and leads to my problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900