I already made a project which is running fine. I copied a class of this project to other project which is creating problem.
Inside the class I have a structure like
struct Test{
CString m_path;
...
}
There is one function in class which returns the object of this structure like
Test& GetStructObj(){return m_strct;};

Further in other function of dialog class, I am doing
Test instImg = GetInstance();;
Test::Image& fImgPath = instImg.GetFilePath();
Error comes in above line. I used watch on instImg where the value of path is correct
but value that comes after assignment is corrupted.
fImgPath.path is coming as

When you do this:
test frImg = instImg.GetStructObj();
What is instImg? Can it be a reference to an instance of whatever class contains that GetStructObj method? If it is, are you sure it is referencing a valid object?

test frImg = instImg.GetStructObj();
is same as
Test::Image& fImgPath = instImg.GetFilePath();
instImg is the instance of singleton class Test.
Actually I can't post the actual code. With the dummy code I am trying to explain the problem.

How about my other two questions?
"When does the code run?" and "are you sure it has been constructed correctly by the time the call is made"?
What i mean is, e.g. you have 2 static objects, Obj1 and Obj2. Obj1's constructor uses Obj2 for something. This will only work if Obj2 has been constructed before Obj1. In one of our projects we ran into this, things were going fine but we did some changes to the project -can't remember anymore what- and all of a sudden it would crash even before it would hit 'main'. It would try to access all bogus pointers and values somewhere, it turned out that whatever the change was we made it modified the order of static object initialization. Could something similar be happening to you?

All right, was worth a try i guess. :)
Put a breakpoint on that line and check if the CString is OK or not just before the code executes. If it is bad then the problem is somewhere else, maybe something somewhere overwrites its memory area (e.g. by indexing out of an array or somesuch).

Classes and structures behave identical with the only differences that members are public by default for structures and private for classes. So it must not be converted (I prefer classes with object members to indicate this).
If no copy operator is specified, there will be a default one. If the problem comes now when calling the CString copy operator, the error must be somewhere else. May be your CString object is damaged.

Be sure that: - Objects that you return are still alive - If you are using singletons, that you use the right object (and not a new copy). - That there aren´t other methods corrupting (writing) the class where the CString lies. - If using static variables, have you declared them correctly? My guess is that your problem is there. If you want to use a "global" instance of a class declare it as "extern" in a header file and normal in a cpp file, like:

extern Test gTest; /*In a global .h*/
Test gTest; /*In a cpp that is used before invoking the variable.*/

Solution 4

Here are a few ideas, which may or may not be correct:
- You are dealing with 2 or more DLLs where there are differences in understanding about the CString
o one DLL is debug and another is Release
o one DLL is compiled with one version of the compiler, the other with a different version
o there is a difference in default alignment between the 2 DLLs.
o one DLL is managed, one not
- Is there now a thread of execution where the struct is not initialized before getting the instance? Does the object actually exist?

Solution 5

If the "m_strct" in your function GetStructObj() is a local variable then the error message coming makes sense because you are trying to read a local variable`s value, which does not exists after function returns, through a reference.