Release 模式下不传递字符串参数

发布于 2024-10-06 01:13:09 字数 857 浏览 3 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2024-10-13 01:13:09

结果发现,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文