如何针对特定情况调试和修复此双自由内存损坏问题

How to debug and fix this double free memory corruption issue for specific case

本文关键字:自由 内存 问题 损坏 何针 调试 情况      更新时间:2023-10-16

当我退出该工具时,我有一个基于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    

我需要一些输入来调试这个问题。我也不能使用金刚砂。它在这种情况下崩溃

您的一个删除或释放调用是在已经释放的内存上完成的。仔细检查所有手动内存分配,检查它们是否正确(删除可疑的内存分配,看看它是否会改变行为(。

理想情况下,使用现代C++,您应该很少或不需要显式的new和delete(使用std提供的智能指针(。

关于Qt,请注意Qt使用父母-子女所有权。您应该只删除"顶部"对象(没有"父对象"集的对象(,Qt将单独删除已删除对象的所有子对象。即,如果您手动分配了一个QObject派生实例,并以某种方式将其链接到QObject层次结构中(添加子对象/设置父对象等(,则直接删除该对象会造成麻烦。