Release 模式下不传递字符串参数
我有一个使用 VS2008 构建的 C++/CLI 库,它使用 marshal_as 将 System::String 方法参数转换为 std::string 类型,以传递到本机指针的方法参数中。该代码看起来与此非常相似:
System::Void QueryContext::SetNamespace(String^ prefix, String^ uri)
{
std::string _prefix;
if (nullptr != prefix)
{
_prefix = marshal_as<std::string>(prefix);
}
std::string _ns;
if (nullptr != uri)
{
_ns = marshal_as<std::string>(uri);
}
// at this point both variables are confirmed to have values
// at least from within the Locals view in the debugger
_ctx->setNamespace(_prefix,_ns);
}
此代码正在针对 x64 平台进行编译。
我目前遇到的问题是:当代码在调试模式下构建时,它运行没有任何问题。当代码在发布模式下构建时,本机指针 (_ctx) 会抛出一个异常,基本上表示没有值分配给变量 _ns,尽管我已经能够在调试器中确认该值确实存在。
换句话说,本机代码在调试模式下运行良好,但在发布模式下会失败,因为本机代码中的值显示为空白。
发布模式下或 marshal_as 模板中是否发生了某些情况,从而导致了问题?以前有人遇到过这种问题吗?
谢谢!
I have a C++/CLI library build with VS2008 that uses marshal_as to convert System::String method parameters into std::string types to pass into a native pointer's method parameters. The code looks very similar to this:
System::Void QueryContext::SetNamespace(String^ prefix, String^ uri)
{
std::string _prefix;
if (nullptr != prefix)
{
_prefix = marshal_as<std::string>(prefix);
}
std::string _ns;
if (nullptr != uri)
{
_ns = marshal_as<std::string>(uri);
}
// at this point both variables are confirmed to have values
// at least from within the Locals view in the debugger
_ctx->setNamespace(_prefix,_ns);
}
This code is being compiled for the x64 platform.
The problem I'm currently having is this: when the code is built in Debug mode, it runs without any problems. When the code is built in Release mode, the native pointer (_ctx) throws an exception basically saying that there is no value assigned to variable _ns, despite the fact that I've been able to confirm in the debugger that the value is indeed there.
In other words, the native code runs fine in Debug mode, but in Release mode it fails because the values appear blank inside the native code.
Is there something going on in Release mode, or with the marshal_as template, that is causing problems here? Has anyone run into this kind of problem before?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果发现,Release 库链接到了 C++->Code Generation 设置中的多线程 Debug DLL (/MDd),而不是链接到 C++->Code Generation 设置中的正确多线程 DLL (/MD)。项目设置。
Turns out the Release library was linked to the Multi-Threaded Debug DLL (/MDd) in the C++->Code Generation settings, as opposed to the correct Multi-Threaded DLL (/MD) in the project settings.