Array Created Statically in DLL Gets Overwrittend by calling Program
I have a main program (PMAIN) that uses dinamically a DLL. Let's say that PMAIN uses 2 functions (foo1, foo2 and foo3) exported by the DLL. The function are something like this:
int __stdcall foo1(){
str = new MyStruct;
}
int __stdcall foo2(){
str.LC->objF1();
}
int __stdcall foo3(int val){
str.LC->objF2(val);
}
MyStruct is like this:
struct MyStruct{
MyObject LC
}
And MyObject is:
class MyObject{
private:
int arr1[100];
int arr2[100];
int Index;
public:
MyObject();
~MyObject();
void objF1();
void objF2(int val);
}
void MyObject::objF1(){
for(int i=0;i<100;i++){
arr1[i]=1;
}
}
void MyObject::objF2(int val){
Index++;
arr2[Index]=val;
}
MyObje开发者_运维知识库ct::MyObject(){
for(int i=0;i<100;i++){
arr1[i]=0;
arr2[i]=0;
}
Index=-1;
}
From the PMAIN I call first foo1 then foo2 then I cycle 100 times calling foo3; the array arr1 gets updated correctly and 'retains' the value during the whole execution of PMAIN while every time I call foo3 the array arr2 contains only zeroes it gets updated and when the program calls foo3 once again it's again blank. Some how the PMAIN overwrites the addresses of the array in the DLL (I saw this behavior running the debug).
Do you know How is it even possible?
aren't the PMAIN and the DLL supposed to be in two different places in the memory?
The variables that are created in your DLL are the same one's the program uses, so it can overwrite them, if there is an error in your program.
This looks wrong to me:
int __stdcall foo1(){
str = new MyStruct;
}
int __stdcall foo2(){
str.LC->objF1();
}
int __stdcall foo3(int val){
str.LC->objF2(val);
}
If str is a pointer, (since you are new'ing it) then you need to use the operator-> to access it's members. Like so:
int __stdcall foo1(){
str = new MyStruct;
}
int __stdcall foo2(){
str->LC.objF1();
}
int __stdcall foo3(int val){
str->LC.objF2(val);
}
and since LC is not a pointer, but just an automatic variable in your struct, then that needs to use the . operator
. As corrected above.
精彩评论