Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;};

Somewhere in dialog class I am doing
test frImg = instImg.GetStructObj();
On this line exception is coming
Unhandled exception at 0x006cb4a8 in TestApp.exe: 0xC0000005: Access violation reading location 0xfffffff0.

I have seen in call stack exception comes at this function
C#
static CStringData* __cdecl CloneData(_Inout_ CStringData* pData)
{
    CStringData* pNewData = NULL;

    IAtlStringMgr* pNewStringMgr = pData->pStringMgr->Clone();

Before this point call stack shows that at Copy constructor of structure Test, the CString data is coming as Bad pointer.
Disassembly shows this information
ASM
Engine::TestEdit::Test::Test:
006EF550  push        ebp
006EF551  mov         ebp,esp
006EF553  push        0FFFFFFFFh
006EF555  push        offset __ehhandler$??0Image@ImageEdit@ImageEngine@@QAE@ABU012@@Z (0BCA3E8h)
006EF55A  mov         eax,dword ptr fs:[00000000h]
006EF560  push        eax
006EF561  sub         esp,0CCh
006EF567  push        ebx
006EF568  push        esi
006EF569  push        edi
006EF56A  push        ecx
006EF56B  lea         edi,[ebp-0D8h]
006EF571  mov         ecx,33h
006EF576  mov         eax,0CCCCCCCCh
006EF57B  rep stos    dword ptr es:[edi]
006EF57D  pop         ecx
006EF57E  mov         eax,dword ptr [___security_cookie (0CEFD9Ch)]
006EF583  xor         eax,ebp
006EF585  push        eax
006EF586  lea         eax,[ebp-0Ch]
006EF589  mov         dword ptr fs:[00000000h],eax
006EF58F  mov         dword ptr [ebp-14h],ecx
006EF592  mov         eax,dword ptr [ebp+8]
006EF595  push        eax
006EF596  mov         ecx,dword ptr [ebp-14h]
006EF599  call        ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > > (69D06Eh)
006EF59E  mov         dword ptr [ebp-4],0



I am totally wondered about the problem as the same class is working in other project but not working in this project.
Posted
Comments
Ravikant 13-Jan-12 4:13am    
This if in .h file

class Test{
struct Image{
CString path;
};

private:
Image imgFile;
public:
Image& GetFilePath(){return imgFile;};
LoadFile(Image& img, const CString& imgPath);
LoadImage(const CString& imgPath);
};

in .cpp file I am using like:
Test::LoadFile(Image& img, const CString& imgPath)
{
img.path = imgPath;
}
Test::LoadImage(const CString& imgPath){
LoadFile(imgFile, imgPath);
}

Somewhere in dialog class I am using
Test instImg = GetInstance();
instImg.LoadImage(_T("C:\\test.jpg"));

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 <badptr>

Note: Test is singleton class.
Code-o-mat 13-Jan-12 4:30am    
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?
Ravikant 13-Jan-12 4:38am    
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.
Code-o-mat 13-Jan-12 4:45am    
When does that code run? Is instImg maybe a "global/static" object? If it ia, are you sure it has been constructed correctly by the time the call is made?
Ravikant 13-Jan-12 5:05am    
isntImg is static object. instImg is the instance of singleton class Test.

The line
C++
Test frImg = instImg.GetStructObj();

creates a new Test structure and tries to copy the content of the referenced structure. But copying fails because your structure contains a CString object.

If you don't need a copy of the structure, but the reference, use
C++
Test& frImg = instImg.GetStructObj();


If you need a copy, you can change the structure to a class and implement a copy operator:

C++
class CTest
{
    Test();
    CString m_path;
    CTest& operator=(const CTest& test);
};
CTest& CTest::operator=(const CTest& test)
{
    m_path = test.m_path;
    return *this;
}
 
Share this answer
 
Comments
Ravikant 12-Jan-12 6:19am    
I don't understand why copying fails in case of CString object in this project whereas in other project it is wokring fine.

I have overladed assignment operator and also added copy constructor inside a structure but still problem persists. Problem comes at line
m_path = test.m_path;
This time problem comes at
CSimpleStringT& operator=(_In_ const CSimpleStringT& strSrc)
{
CStringData* pSrcData = strSrc.GetData();
CStringData* pOldData = GetData();
if( pSrcData != pOldData)
{
if( pOldData->IsLocked() || pSrcData->pStringMgr != pOldData->pStringMgr )

Is it necessary to convert structure to class?
Jochen Arndt 12-Jan-12 6:49am    
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.
The code you posted works.

You should check the value you assigned the string you are trying to copy.
There may be an string allocation error on the source string (m_strct.m_path).
 
Share this answer
 
Comments
Ravikant 13-Jan-12 2:34am    
may be u r right...
but code is working fine in some other place.

can u pls tell me how to avoid string allocation error in this case?
ErnestoNet 20-Jan-12 9:12am    
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.*/

I don't recommend global variables, but....
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?
 
Share this answer
 
Comments
nv3 8-Apr-13 9:26am    
Have you taken a look at the date of the request ;-)
H.Brydon 8-Apr-13 10:25am    
Ha ha - cute. Looks like Solution #3 was added recently, which pushed it to the top of the 'recent' issues.
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.
 
Share this answer
 

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