您能解释一下此C ++删除问题吗? [英] Can you please explain this C++ delete problem?

查看:116
本文介绍了您能解释一下此C ++删除问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

std::string F()
{
  WideString ws = GetMyWideString();

  std::string ret;
  StringUtils::ConvertWideStringToUTF8(ws, ret);
  return ret;
}

WideString是第三方类,StringUtils也是如此.他们对我来说是一个黑匣子.第二个参数是通过引用传递的.

WideString is a third-party class, so are StringUtils. They are a blackbox to me. Second parameter is passed by reference.

当我逐步通过调试器时,行return ret会抛出一个讨厌的弹出窗口(Visual C ++),表示堆可能已损坏.仔细检查后,返回的字符串的副本就可以了,但是ret的删除失败. ret在返回之前包含正确的值.

When I step through the debugger the line return ret throws a nasty popup (Visual C++) saying that heap may be corrupted. Upon closer examination copy of the string that gets returned is OK, but the deletion of ret fails. ret contains correct value before return.

转换功能可能导致此问题吗?有什么要解决的想法吗?

What could the converting function possibly do to cause this? Any ideas to fix?

更新:

  • 项目本身是一个dll
  • StringUtils是一个库
  • 项目是针对多线程CRT(不是调试,不是dll)编译的
  • 程序在Visual Studio外部运行时似乎运行正常

推荐答案

  1. 如果StringUtils是单独编译的(例如,使用不同的编译器版本),则对象布局可能会发生冲突.
  2. 如果StringUtils在DLL中,则必须确保将它和主程序都编译为使用DLL中的标准库.否则,每个模块(可执行文件和DLL)都会有自己的堆.当StringUtils尝试处理从其他堆分配的字符串中的数据时,会发生不好的事情.

这篇关于您能解释一下此C ++删除问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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