c++ - 如何针对特定情况调试和修复此双重可用内存损坏问题
问题描述
退出工具时,我有一个基于 QT 的 GUI,出现以下问题
*** glibc detected *** /bin/linux_x86_64/main-g: double free or corruption
(!prev): 0x00000000049eca50 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3feee75e66]
/lib64/libc.so.6[0x3feee789b3]
/linux_x86_64/main2-
g(_ZN9__gnu_cxx13new_allocatorIPN3zi_14PCommandEE10deallocateEPS3_m+0x20)
[0x1d9f504]
gui//../bin/linux_x86_64/main2-g
_ZNSt16allocator_traitsISaIPN3zi_14PCommandEEE10deallocateERS3_PS2_m+0x2b)
[0x1d9f0de]
/gui//../bin/linux_x86_64/main2-g
(_ZNSt12_Vector_baseIPN3zi_14PCommandESaIS2_EE13_M_deallocateEPS2_m+0x32)
[0x1d9eba6]
/gui//../bin/linux_x86_64/main2-
g(_ZNSt12_Vector_baseIPN3zi_14PCommandESaIS2_EED2Ev+0x41)[0x1d9e955]
../bin/linux_x86_64/main2-g(_ZNSt6vectorIPN3zi_14PCommandESaIS2_EED1Ev+0x41)
[0x1dc771b]
/lib64/libc.so.6(__cxa_finalize+0x9d)[0x3feee35ebd]
../lib/linux_x86_64/lib-g.so(+0x3a627c6)[0x7f63
====== Memory map: ========
00400000-02b5f000 r-xp 00000000 08:03 21767481
../main2-g
02d5e000-02f8c000 rw-p 0275e000 08:03 21767481
../main2-g
02f8c000-03222000 rw-p 00000000 00:00 0
03cc7000-04cac000 rw-p 00000000 00:00 0
我需要一些输入来调试这个问题。我也无法使用 Valgrind 。在这种情况下它崩溃了
解决方案
您的删除或免费调用之一是在已释放的内存上完成的。仔细检查所有手动内存释放并检查它们是否正确(删除可疑的并查看它是否会改变行为)。
理想情况下,使用现代 C++,您应该很少或不需要显式的 new 和 delete(使用 std 提供的智能指针)。
关于 Qt,请注意 Qt 使用父子所有权。您应该只删除“顶级”对象(没有设置“父”的对象),Qt 将自行删除已删除对象的所有子对象。即,如果您手动分配了 QObject 派生实例并以某种方式将其链接到 QObject 层次结构中(添加子/设置父/等),直接删除该对象将导致麻烦。
推荐阅读
- c# - Entity Framework 实体中的元素与 Linq 相交
- opencv - OpenCV 的 Rust 绑定似乎缺少面部模块
- django - 无法设置 django-oauth-toolkit 身份验证
- css - ReactJs 中的图像悬停效果
- javascript - 在 Javascript 中枚举和排序多个 Promise
- javascript - Mongoose 在 Apollo-Express 服务器上抛出错误 ECONNREFUSED
- python - 按下 ctrl 和 shift 时 Pygame 未检测到 a 键
- python - Python:如何在 n 个长列表中找到三个元素的所有可能组合
- angular - 为 ApplicationRef 检测到 DI 中的循环依赖性。如何修复它
- python - 使用 cross_validate 生成混淆矩阵