Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C++/CLI C#
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
Posted 17-Apr-13 22:51pm
Comments
jsolutions_uk at 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 :)
Lygatto Zenki at 18-Apr-13 11:16am
   
I'm sorry that the code belongs to a project ,so i need to keep secret......
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I got the method from here.http://stackoverflow.com/questions/57840/how-to-attach-debugger-to-step-into-native-c-code-from-a-managed-c-wrappe[^]
but i want to fix the exception , i am trying .
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
you should use WinDbg
 
Quick-start-to-using-WinDbg
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Comments
Lygatto Zenki at 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)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100