Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i use dllimport to invoke C++ function as below:

C#
[DllImport("Test.dll", EntryPoint = "Init", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int Init();


i use this function serveral months,after that,i add one line code in Init as below:
C++
A* a = new A();


when i use VS2010 debug Run model is ok,but when i use double click to run exe,it throw error:
Try to read or write protected memory. This usually indicates that other memory is corrupt


i can't find any problem in the code,very confuse.
so post this question and hope someone met the same question and help me to solve.
thanks!

What I have tried:

I add two messagebox as below:
C++
MessageBox(NULL,"111111!","OK!",MB_OK | MB_ICONINFORMATION);
A* a = new A();
MessageBox(NULL,"222222!","OK!",MB_OK | MB_ICONINFORMATION);

so compile code project,and double click EXE,"111111" can popup,but "22222" cann't be.
so i'm sure that new A() must be some error in.

A class consturct function as below:
C++
A::A()
{
        MessageBox(NULL,"333333!","OK!",MB_OK | MB_ICONINFORMATION);
	ctx = new des_context();
	ctx3 = new des3_context();
	bDESKeyExists = false;
	bDES3KeyExists = false;
}

i cann't see "333333" popup,very confuse,cann't find the question.
Posted
Updated 28-Jul-17 0:54am
v2
Comments
Sergey Alexandrovich Kryukov 12-Jun-16 22:43pm    
Not enough information. The exception is, most likely, thrown on the unmanaged part.
The worst thing is: you don't destruct A* a. Do you do delete a anywhere?
—SA
xuyunhai 12-Jun-16 22:46pm    
i'm not destruct A* a,just to find question,so add messagebox to popup "333333",but it cann't popup "333333",so i'm very confuse and cann't find question.
Sergey Alexandrovich Kryukov 12-Jun-16 22:52pm    
I can see it, but that's still a problem. Are you going to delete a?
—SA
xuyunhai 12-Jun-16 22:55pm    
no, a must exist,because after new A(),i use many functions in A class.
Sergey Alexandrovich Kryukov 12-Jun-16 22:59pm    
All wrong. Each new should come with delete. It depends on different factors where to delete it, but you cannot leave it. This is wrong code design in general. Please eliminate this problem.
—SA

1 solution

Looks like the constructor of A() has some issues. Is a child class and its parent has the issue or maybe some constructors of class members.

The error message sounds like some read/write on garbage memory or pointers is done.

Build the A() as debug build and use the debugger with "step into" on the A() constructor. It works!!!
 
Share this answer
 
Comments
Richard MacCutchan 28-Jul-17 8:11am    
Look at the date of the question.

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