C++ std::string 参数是 <Bad Ptr>

标签 c++ visual-c++

我使用的是 Visual Studio 2010。

我有一个具有以下构造函数的类:

CVideoAnnotation::CVideoAnnotation(std::string aPort, DWORD aBaudRate)

我创建了一个 CVideoAnnotation 实例,如下所示:

CVideoAnnotation cVideoAnnotation("COM3", CBR_9600);

“CBR_9600”是解析为 9600 的宏。

在构造函数中,aBaudRate 为预期的 9600。但是,aPort 没有正确传递。当我将光标悬停在它上面时,IntelliSense 给出的值为 <Bad Ptr> .

有没有人对为什么字符串没有正确传递有任何想法?

谢谢, 戴夫

作为对我原来问题的更新,我在构造函数中添加了构造函数调用和局部人口的汇编代码。

   CVideoAnnotation cVideoAnnotation("COM3", CBR_9600);
0041177D  push        2580h  
00411782  sub         esp,20h  
00411785  mov         ecx,esp  
00411787  mov         dword ptr [ebp-174h],esp  
0041178D  push        offset string "COM3" (4198C8h)  
00411792  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > (41131Bh)  
00411797  mov         dword ptr [ebp-17Ch],eax  
0041179D  lea         ecx,[ebp-11h]  
004117A0  call        dword ptr [__imp_CVideoAnnotation::CVideoAnnotation (41D4DCh)]  
004117A6  mov         dword ptr [ebp-180h],eax  
004117AC  mov         dword ptr [ebp-4],0  





CVideoAnnotation::CVideoAnnotation(std::string aPort, DWORD aBaudRate)
{
100137F0  push        ebp  
100137F1  mov         ebp,esp  
100137F3  push        0FFFFFFFFh  
100137F5  push        offset __ehhandler$??0CVideoAnnotation@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@K@Z (1001DC82h)  
100137FA  mov         eax,dword ptr fs:[00000000h]  
10013800  push        eax  
10013801  sub         esp,164h  
10013807  push        ebx  
10013808  push        esi  
10013809  push        edi  
1001380A  push        ecx  
1001380B  lea         edi,[ebp-170h]  
10013811  mov         ecx,59h  
10013816  mov         eax,0CCCCCCCCh  
1001381B  rep stos    dword ptr es:[edi]  
1001381D  pop         ecx  
1001381E  mov         eax,dword ptr [___security_cookie (10026090h)]  
10013823  xor         eax,ebp  
10013825  mov         dword ptr [ebp-10h],eax  
10013828  push        eax  
10013829  lea         eax,[ebp-0Ch]  
1001382C  mov         dword ptr fs:[00000000h],eax  
10013832  mov         dword ptr [ebp-18h],ecx  
10013835  mov         dword ptr [ebp-84h],0  
1001383F  mov         dword ptr [ebp-4],0  

最佳答案

如果您在单独的 DLL 中实现 CVideoAnnotation,那么在使用 STL 容器时您会遇到一个众所周知的跨越 DLL 边界的问题。为了验证情况是否如此,创建一个采用 const char* 而不是 std::string 的新构造函数并尝试..

另一件事,而不是 std::string 更喜欢使用 const std::string&

关于C++ std::string 参数是 <Bad Ptr>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4320172/

相关文章:

c# - Visual-C++ DLL 将 2-3 兆字节复制到 C# 缓冲区的最快方法是什么?

c++ - 迭代器成员行为

c++ - 编写可测试代码 - lambda 函数和 unique_ptr 中的 basic_istream 工厂

c++ - 直接从输入缓冲区以相反顺序读取数组

windows - 如何在属性页中获得正确的背景和控件颜色?

c++ - 错误 C2678 : binary '!=' : no operator found

c++ - 在组合框中选择项目时显示格式化文本

c++ - 向 vector 添加元素

visual-c++ - 无法在 32 位计算机上注册使用 VS2012/VC++ 构建的 COM DLL

visual-studio - “Call Stack” for Visual Studio 2005中的C++错误