我正在修改用Qt编写的GUI代码(
LibrePilot
). 有一种方法可以添加用户类。它们是根据XML描述自动生成的。
所以这个班:
<xml>
<object name="RecorderService_RecordDescription" singleinstance="true" settings="false" category="State">
<description>For Recorder gadget</description>
<field name="text" units="char" type="string" elements="1"/>
<access gcs="readwrite" flight="readwrite"/>
<telemetrygcs acked="false" updatemode="manual" period="0"/>
<telemetryflight acked="false" updatemode="periodic" period="1000"/>
<logging updatemode="periodic" period="100"/>
</object>
</xml>
// Field structure
typedef struct {
QString text;
} __attribute__((packed)) DataFields;
当我试图设置
text
setText
方法:
void RecorderService_RecordDescription::setText(const QString value)
{
mutex->lock();
bool changed = (data_.text != static_cast<QString>(value));
data_.text = static_cast<QString>(value);
mutex->unlock();
if (changed) { emit textChanged(value); }
}
我明白了
SIGABRT
例外。
#0 0x00007ffff509ce97 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff509e801 in __GI_abort () at abort.c:79
#2 0x00007ffff50e7897 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff5214b9a "%s\n") at ../sysdeps/posix/libc_fatal.c:181
#3 0x00007ffff50ee90a in malloc_printerr (str=str@entry=0x7ffff5212d88 "free(): invalid pointer") at malloc.c:5350
#4 0x00007ffff50f5e1c in _int_free (have_lock=0, p=0x7ffff5f8c5f0, av=0x7ffff5449c40 <main_arena>) at malloc.c:4157
#5 0x00007ffff50f5e1c in __GI___libc_free (mem=0x7ffff5f8c600) at malloc.c:3124
#6 0x00007fffc798671e in RecorderService_RecordDescription::setText(QString) () at /usr/lib/librepilot-gcs/plugins/LibrePilot/libUAVObjects.so
我发现的一个解决方案是,如果我将另一个字段添加到
RecorderService_RecordDescription
<xml>
<object name="RecorderService_RecordDescription" singleinstance="true" settings="false" category="State">
<description>For Recorder gadget</description>
<field name="text" units="char" type="string" elements="1"/>
<field name="seconds" units="sec" type="uint32" elements="1"/>
<access gcs="readwrite" flight="readwrite"/>
<telemetrygcs acked="false" updatemode="manual" period="0"/>
<telemetryflight acked="false" updatemode="periodic" period="1000"/>
<logging updatemode="periodic" period="100"/>
</object>
</xml>
错误消失了。自动生成的类具有以下数据结构:
// Field structure
typedef struct {
quint32 seconds;
QString text;
} __attribute__((packed)) DataFields;
的代码
是一样的。
这个错误在我们使用旧版本的gcc之前没有发生过。当前版本是7.2。
任何人都知道错误发生的原因以及如何优雅地修复它。
谢谢您。