in

AisNote Software

The software can improve your computer work

Great Windows Tool

CString Crash in ~CString.在不通工程之间调用crash的问题

Recently,I met a question as below:

One is DLL:

CString GetString()

{

return _T(“test”);

}

 

One is application to call this DLL’s api:

CString strTest = GetString();

in deconstructor,app will crash.

inline CString::~CString()

//  free any attached data

{

if (GetData() != _atltmpDataNil)

{

if (InterlockedDecrement(&GetData()->nRefs) <= 0)

delete[] (BYTE*)GetData();

}

}

Root cause: app is /MD,Dll is /M

Solution: set the code-generaltion to the same type.

参考://20090527

CString的一个问题

CString的Release函数实现如下:

void CString::Release()
{
        if (GetData() != _afxDataNil)
        {
                ASSERT(GetData()->nRefs != 0);
                if (InterlockedDecrement(&GetData()->nRefs) <= 0)
                        FreeData(GetData());
                Init();
        }
}
其中_afxDataNil定义如下:
  AFX_STATIC_DATA int _afxInitData[] = { -1, 0, 0, 0 };
AFX_STATIC_DATA CStringData* _afxDataNil = (CStringData*)&_afxInitData;

sizeof(CString)等于4,它是一个指针。对于空的CString,MFC会让其指向一个静态数据_afxInitData。MFC通过判断CString的值是否等于_afxInitData的地址来判断CSting是否是空的。

如果在exe和dll之间传递CString对象,而exe和dll都与MFC静态链接,那么在exe和dll中的_afxInitData的地址可能是不同的。exe中的空CString传到dll中会被当成非空CString处理,然后就会发生错误。

一般而言,如果不是动态链接MFC类库,应尽量避免在exe和dll之间传递MFC的对象。

Published May 27 2009, 04:19 PM by elliotllb
Filed under:

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add
aisnote software
Powered by Community Server (Non-Commercial Edition), by Telligent Systems