STL string 析构异常原因分析
2011-08-23 16:21
423 查看
最近已经遇到这个问题两次了,看了网上一些资料了解了下,引用两篇过来,以免忘记
文章一:出处http://hi.baidu.com/anowsober/blog/item/9c1abcd95d20b4ee38012f6b.html
一次 STL string 析构异常原因分析
2009-01-23 16:31
文章一:出处http://hi.baidu.com/anowsober/blog/item/9c1abcd95d20b4ee38012f6b.html
一次 STL string 析构异常原因分析
2009-01-23 16:31
本剧关键字:exe、dll、STL string、local heap。 情景:一个结构体 ITEM 中包含了多个 string 类型字段,在 exe 中的 ExeFunc 中定义 ITEM 变量 item,传 item 的引用到 DllFunc 函数中,DllFunc 是一个 dll 的导出函数,负责从文件中读取一些数据到 ITEM 结构中。在 ExeFunc 结束的时候 string 异常了,提示堆内存损坏。排除法发现多个 string 字段中只有一个引发了异常,其它的都没有问题,觉得很奇怪。string 异常的字段赋值的字符串大于 16 个字符(string 默认是15 还是 16 个 byte 来着)其它的都小于 16 个字符,显然是 string 对象重新分配内存了。 查看内存也没有发现异常情况,怀疑 string 有问题,于是另外建了个工程测试 string,单个 exe 或者模拟工程中建立一个 dll 赋值给引用 string 变量都正常,而且字符数大于 16 个。为什么为什么为什么呢?同样是重新分配了内存,差别怎么就这么大捏。 跟踪定位到 _CrtIsValidHeapPointer ,注意到 dbgheap.c 文件中 _CrtIsValidHeapPointer 处注释: /* * If this ASSERT fails, a bad pointer has been passed in. It may be * totally bogus, or it may have been allocated from another heap. * The pointer MUST come from the 'local' heap. */ _ASSERTE(_CrtIsValidHeapPointer(pUserData)); 搜索到一些关于本地堆(local heap)的文章,才明白是因为 dll 如果静态链接了运行时库,dll 就会拥有独立于应用程序堆(也称作local heap)的运行时堆实例。此时在 dll 外部就不能访问此 local heap,所以也就有上面所出现的异常啦。MSDN 中也有介绍: The _CrtIsValidHeapPointer function is used to ensure that a specific memory address is within the local heap. The local heap refers to the heap created and managed by a particular instance of the C run-time library. If a dynamic-link library (DLL) contains a static link to the run-time library, it has its own instance of the run-time heap, and therefore its own heap, independent of the application's local heap. When _DEBUG is not defined, calls to_CrtIsValidHeapPointer are removed during preprocessing. 查找这个问题浪费了不少的时间,解决办法就是在编译 dll 时动态链接运行时库( VS2005 下动态链接 MFC 库默认就是动态链接运行时库,如果更改静态链接 MFC 库,运行时库的链接方式也跟着改变为静态的),并且 exe 中也要使用相同的链接方式才行。这或许是有时候改变 MFC 库的链接方式会引起一些莫名错误的原因吧;对了在 _CrtIsValidHeapPointer 内部也有这样的注释: /* * Go through the heap regions and see if the pointer lies within one * of the regions of the local heap. * * Pointers from non-local heaps cannot be handled. For example, a * non-local pointer may come from a DLL that has the CRT linked-in. * */ 结论:应用程序使用的 dll 库在链接时尽量使用相同的链接方式,大多数情况下不同的链接方式不会出现问题,但有少数情况总是存在滴。俺要是在第一时间看清楚明了的 MSDN 就不会走N多的弯路了555(在弯路上也认识了不少东西算是弥补吧呵呵)。 文章二:出处http://topic.csdn.net/u/20091202/09/999d3d15-cec4-4e40-8ad3-e1f35442d9ef.html 动态库接口层使用STL的运行异常及一个解决思路
|
相关文章推荐
- STL string 析构异常原因分析
- Windbg定位异常系列 - 给被调用函数的stl string类型参数传递了空指针引发的崩溃(windbg结合IDA一起分析)
- C++ Primer 学习笔记_31_STL实践与分析(5) --再谈string类型(上)
- g++编译报错原因分析 expected unqualified-id before string constant
- C++(13)STL实践与分析之再谈String类型
- C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
- hive执行任务时异常终止原因分析
- 通过JVM堆栈分析线程出现大量异常的原因
- string源码分析 ——转载 http://blogs.360.cn/360cloud/2012/11/26/linux-gcc-stl-string-in-depth/
- 解决mysql插入数据时出现Incorrect string value: '\xF0\x9F...' for column 'name' at row 1的异常 这个问题,原因是UTF-8编码有可
- TOP语句放到表值函数外,效率异常低下的原因分析
- Android 3.0获取互联网资源时出现异常的原因分析以及解决方案
- C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- 修改List报ConcurrentModificationException异常原因分析
- 结构体中含有string 会异常的原因和解决办法
- 在Activity的onCreate方法中显示PopupWindow导致异常的原因分析及解决方案
- vc6程序异常崩溃 - stl的basic_string内存破坏
- ElasticSearch排序引起的all shards failed异常原因分析
- 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。 (异常来自 HRESULT:0x8000401A)。 在 BatchImportEntryTable.GetExcelData(String FileName)