Click here to Skip to main content
13,090,930 members (47,386 online)
Rate this:
Please Sign up or sign in to vote.
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
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
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 11-Jan-12 23:17pm
Ravikant 13-Jan-12 4:13am
This if in .h file

class Test{
struct Image{
CString path;

Image imgFile;
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();

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.
Code-o-mat 13-Jan-12 5:31am
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?
Ravikant 13-Jan-12 6:18am
I have only single object of Test class. Code runs when button is clicked on dialog class.
Code-o-mat 13-Jan-12 6:34am
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).
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The line
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
Test& frImg = instImg.GetStructObj();

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

class CTest
    CString m_path;
    CTest& operator=(const CTest& test);
CTest& CTest::operator=(const CTest& test)
    m_path = test.m_path;
    return *this;
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

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

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2017
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