字符串复制和cstring,basic_string和amp;之间的转换_bstr_t抛出异常并使应用程序崩溃 [英] string copy and conversions between cstring, basic_string & _bstr_t throws exception and crash the application

查看:112
本文介绍了字符串复制和cstring,basic_string和amp;之间的转换_bstr_t抛出异常并使应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于在MSVS 2015中的字符串之间复制数据时的内存分配问题(64位,windows 7)。 xstring&中抛出异常C:/ Program Files(x86)/ MSVS
12.0 / VC / Include中的xmemory文件。为了重新创建崩溃我为我的应用程序启用了gflags(32位)并以调试模式运行。基本上,工作流程涉及以RTF格式从剪贴板复制数据,该格式将cstring返回给类对象,该对象采用basic_string< chartype>
参考参考。 chartype是TCHAR或wchar_t,或者只是char。异常或崩溃始终在类的构造函数中,其中传递来自剪贴板的输入字符串。除了这个很多其他地方我也遇到了
相同调用堆栈的问题。我已将下面的调用堆栈附加到异常期间以供参考。 请提示。


KERNELBASE!RaiseException + 0x58

msvcr120!_CxxThrowException + 0x5b | [f:\dd\vctools\crt\crtw32\eh\throw.cpp @ 152]
$
mfc120!AfxThrowMemoryException + 0x19 | [f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\except.cpp @ 218]

mfc120!operator new + 0x20 | [ f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxmem.cpp @ 315]
$
NOTESADDDLG!std :: _ Wrap_alloc< std :: allocator > :: allocate + 0x1b | [c:\program files(x86)\ microsoft visual studio 12.0\vc\include\xmemory0 @ 847]

NOTESADDDLG!std :: basic_string< ; wchar_t,std :: char_traits,std :: allocator> :: _ Copy + 0xb6 | [c:\program files(x86)\ microsoft visual studio 12.0\vc\include\xstring @ 2219]

NOTESADDDLG!std :: basic_string< wchar_t,std :: char_traits,std :: allocator> :: assign + 0x8c | [c:\program files(x86)\ microsoft visual studio 12.0\vc\include\xstring @ 1138]


导致此异常的代码看起来像


1.  
//从剪贴板中获取rtf格式数据(返回cstring)


2.  
CString sClipboardRtf =
CClipboardHelper :: GetDataForFormat( CF_RTF
GetSafeHwnd());


3.  
if (!sClipboardRtf.IsEmpty())


4.  
{


5.  
//使用输入rtf进行清理


6.  
CCleanUp < wchar_t >
CleanUpFile((
const < span style ="color:black; font-family:Consolas; font-size:9.5pt">
wchar_t *)( _ bstr_t )(sClipboardRtf));


7.  
// CCleanUp< wchar_t> CleanUpFile((const wchar_t *)CT2W(sClipboardRtf));



8.  
//清理后得到ouptut rtf文本


9.  
sClipboardRtf
= CleanUpFile.GetRtfText()。c_str();


10.
}



如果有两个拷贝构造函数(第6行和第7行),我在其中以cstring格式从剪贴板传递输入rtf(一个被注释)。复制构造函数CCleanUp采用std :: basic_string< charType> ;
参数参考。最后,我在第9行得到输出rtf。所有语句都抛出与上面提到的字符串分配有关的异常。由于第9行引起的异常不一致,但是第6行或第7行总是如此抛出异常。请
建议解决此问题





解决方案

这是关于在MSVS 2015中的字符串之间复制数据时的内存分配问题。在C:/ Program Files(x86)/ MSVS 12.0 / VC / Include中的xstring& xmemory文件中抛出异常。为了重新创建崩溃我为我的应用程序启用了gflags(32
位)并以调试模式运行。基本上,工作流程涉及从剪贴板复制数据RTF格式,它将cstring返回给一个类对象,该对象采用basic_string< chartype>参考参考。 chartype是TCHAR或wchar_t,或者只是char。
异常或崩溃始终在类的构造函数中,其中传递来自剪贴板的输入字符串。除了这个很多其他地方,我也遇到了同一个调用堆栈的问题。我已将下面的调用堆栈附加在
异常期间作为参考。 请建议解决此问题。


KERNELBASE!RaiseException + 0x58

msvcr120!_CxxThrowException + 0x5b | [f:\dd\vctools\crt\crtw32\\\\ throw.cpp @ 152]

mfc120!AfxThrowMemoryException + 0x19 | [f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\except.cpp @ 218] b
mfc120!operator new + 0x20 | [f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxmem.cpp @ 315]

NOTESADDDLG!std :: _ Wrap_alloc< std :: allocator> :: allocate + 0x1b | [c:\program files(x86)\ microsoft visual studio 12.0\vc\include\ xmemory0 @ 847]

NOTESADDDLG!std :: basic_string< wchar_t,std :: char_traits,std :: allocator> :: _ Copy + 0xb6 | [c:\program files(x86)\ microsoft visual studio 12.0\vc\include\xstring @ 2219]

NOTESADDDLG!std :: basic_string< wchar_t,std :: char_traits,std :: allocator> :: assign + 0x8c | [c:\program files(x86)\ microsoft visual studio 12.0 \\ vc\include\xstring @ 1138]


感谢


This is regarding memory allocation issues while copying data between the strings in MSVS 2015 (64 bit, windows 7). the exceptions are thrown in xstring & xmemory files inside C:/Program Files(x86)/MSVS 12.0/VC/Include. To recreate crash I enabled gflags for my application(32 bit) and ran in debug mode. Basically the workflow involves copying data from Clipboard in RTF format which returns cstring to a class object which takes basic_string<chartype> parameter by reference. chartype is either TCHAR or wchar_t or simply char. The exception or the crash is always in the constructor of class where the input string from clipboard is passed. Apart from this many other places also I encountered the issue with the same call stack. I have attached the call stack below for reference during the exception. Please suggest.

KERNELBASE!RaiseException+0x58
msvcr120!_CxxThrowException+0x5b|[f:\dd\vctools\crt\crtw32\eh\throw.cpp @ 152]
mfc120!AfxThrowMemoryException+0x19|[f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\except.cpp @ 218]
mfc120!operator new+0x20|[f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxmem.cpp @ 315]
NOTESADDDLG!std::_Wrap_alloc<std::allocator >::allocate+0x1b|[c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 @ 847]
NOTESADDDLG!std::basic_string<wchar_t,std::char_traits,std::allocator >::_Copy+0xb6|[c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring @ 2219]
NOTESADDDLG!std::basic_string<wchar_t,std::char_traits,std::allocator >::assign+0x8c|[c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring @ 1138]

Code that causes this exception looks like

1.  // Get the rtf format data from clipboard (returns cstring)

2.  CString sClipboardRtf = CClipboardHelper::GetDataForFormat(CF_RTF, GetSafeHwnd());

3.  if (!sClipboardRtf.IsEmpty())

4.  {

5.  // Do some clean up with the input rtf

6.  CCleanUp<wchar_t> CleanUpFile((const wchar_t*)(_bstr_t)(sClipboardRtf));

7.  //CCleanUp<wchar_t> CleanUpFile((const wchar_t*)CT2W(sClipboardRtf));

8.  // After cleanup get the ouptut rtf text

9.  sClipboardRtf = CleanUpFile.GetRtfText().c_str();

10. }

Inside if there are two copy constructors(lines 6 & 7) where I am passing the input rtf from clipboard in cstring format (one is commented). The copy constructor CCleanUp takes std::basic_string<charType> parameter by reference. Finally, I am getting the output rtf in the line 9. All the statements throw exceptions relating to string allocation mentioned above. The exception due to line 9 is not consistent, however lines 6 or 7 always throw exceptions. Please suggest resolution for this

解决方案

This is regarding memory allocation issues while copying data between the strings in MSVS 2015. the exceptions are thrown in xstring & xmemory files inside C:/Program Files(x86)/MSVS 12.0/VC/Include. To recreate crash I enabled gflags for my application(32 bit) and ran in debug mode. Basically the workflow involves copying data from Clipboard in RTF format which returns cstring to a class object which takes basic_string<chartype> parameter by reference. chartype is either TCHAR or wchar_t or simply char. The exception or the crash is always in the constructor of class where the input string from clipboard is passed. Apart from this many other places also I encountered the issue with the same call stack. I have attached the call stack below for reference during the exception. Please suggest resolution for this.

KERNELBASE!RaiseException+0x58
msvcr120!_CxxThrowException+0x5b|[f:\dd\vctools\crt\crtw32\eh\throw.cpp @ 152]
mfc120!AfxThrowMemoryException+0x19|[f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\except.cpp @ 218]
mfc120!operator new+0x20|[f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\afxmem.cpp @ 315]
NOTESADDDLG!std::_Wrap_alloc<std::allocator >::allocate+0x1b|[c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 @ 847]
NOTESADDDLG!std::basic_string<wchar_t,std::char_traits,std::allocator >::_Copy+0xb6|[c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring @ 2219]
NOTESADDDLG!std::basic_string<wchar_t,std::char_traits,std::allocator >::assign+0x8c|[c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring @ 1138]

THANKS


这篇关于字符串复制和cstring,basic_string和amp;之间的转换_bstr_t抛出异常并使应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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