错误的指针被分配给CString对象 [英] Bad pointer is assigned to CString object

查看:105
本文介绍了错误的指针被分配给CString对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个运行良好的项目。我将这个项目的一个类复制到了另一个产生问题的项目中。

在课堂上我有一个类似
的结构
struct Test {

CString m_path;

...

}

类中有一个函数返回此结构的对象,如

测试& GetStructObj(){return m_strct;};



我正在做对话类的某个地方

test frImg = instImg.GetStructObj();

在此行上发生异常

TestApp.exe中0x006cb4a8处的未处理异常:0xC0000005:访问冲突读取位置0xfffffff0。



我在调用堆栈中看到异常来自此函数

  static  CStringData * __cdecl CloneData (_Inout_ CStringData * pData)
{
CStringData * pNewData = NULL;

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



在此之前,调用堆栈显示在Copy构造函数中结构测试,CString数据变为坏指针。

反汇编显示此信息

 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 推送 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 代表 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 调用 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







我完全不知道这个问题,因为同一个班级正在其他项目中工作,但在这个项目中没有工作。

解决方案

?? 0Image @ ImageEdit @ ImageEngine @@ QAE @ ABU012 @@ Z(0BCA3E8h)
006EF55A mov eax ,< span class =code-sdkkeyword> dword ptr fs:[ 00000000h ]
006EF560 推送 eax
006EF561 sub esp ,0CCh
006EF567 push ebx
006EF568 push esi
006EF569 push edi
006EF56A push ecx
006EF56B lea edi ,[ebp-0D8h]
006EF571 < span class =code-keyword> 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 < span class =code-keyword> push eax
006EF596 mov < span class =code-keyword> 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







我完全不知道这个问题,因为同一个班级正在其他项目中工作,但没有在这个项目中工作。


该行

< pre lang =c ++>测试frImg = instImg.GetStructObj();



创建一个新的Test结构并尝试复制引用结构的内容。但是复制失败是因为你的结构包含一个CString对象。



如果你不需要结构的副本,但参考,请使用

测试& frImg = instImg.GetStructObj(); 





如果需要副本,可以将结构更改为类并实现复制运算符:



  class  CTest 
{
测试();
CString m_path;
CTest& operator =( const CTest& test);
};
CTest& CTest :: operator =( const CTest& test)
{
m_path = test.m_path;
返回 * ;
}


您发布的代码有效。



您应该检查值你分配了你要复制的字符串。

源字符串上可能存在字符串分配错误(m_strct.m_path)。


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

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.

解决方案

??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.


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
{
    Test();
    CString m_path;
    CTest& operator=(const CTest& test);
};
CTest& CTest::operator=(const CTest& test)
{
    m_path = test.m_path;
    return *this;
}


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).


这篇关于错误的指针被分配给CString对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆