在minGW上使用qt5.3,在Windows 7上运行。
我有一个奇怪的错误,我得到一个sigsegv取决于我声明一个对象的位置。 出现故障的代码是:
<xs:element name="MyElement">
<xs:complexType>
<xs:sequence>
<xs:element ref="SomeElement" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="MyTestElement">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(Attribute name value) some other regex matching"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
要解释一下,TargetDefinition是一个有一个类的类
QStringList tsetFileNames = dir.entryList(nameFilter);
// now load each in turn and put the name etc into the m_prodList
for (int i = 0; i < tsetFileNames.size(); i++)
{
TargetDefinition target;
if (target.ReadXmlFile(dirName+tsetFileNames.at(i)))
{
ProdListItem_t item;
item.name = target.m_name;
item.tsetFile = tsetFileNames.at(i);
m_prodList.append(item);
} else
qDebug() << "Error updating tset list. file "+tsetFileNames.at(i);
}
成员变量。此列表由ReadXmlFile()函数填充。
我使用调试器只有一个文件(即tsetFileNames.Size()== 1)来查看发生了什么。
使用有效(但为空)的QList创建目标对象。在进入ReadXmlFile()时,我可以看到它填充了正确创建的QList。
然而,当它从ReadXmlFile()退出时(即到达“ProdListItem_t item;”行),我看到对QList的“无法访问”。 然后,当它退出for循环时,TargetDefinition析构函数在遇到foreach循环时会导致异常。
QList<TDefVariant_t*> m_variants;"
我真的很难弄清楚导致QList失效的原因。目标仍然在范围内,没有尝试删除列表。 另外,如果我在循环之外采取声明:
foreach(TDefVariant_t* variant, this->m_variants)
{
delete variant;
}
没有错误就可以正常工作。
我想知道我是否溢出堆栈或类似的东西,但会想到调试器会告诉我这个? 否则,我没有想法。它可能是显而易见的,所以任何人都可以让我摆脱困境吗?
TDefVariant_t定义是:
TargetDefinition target;
for (int i = 0; i < tsetFileNames.size(); i++)
{
if (target.ReadXmlFile(dirName+tsetFileNames.at(i)))
{
ProdListItem_t item;
item.name = target.m_name;
item.tsetFile = tsetFileNames.at(i);
m_prodList.append(item);
} else
qDebug() << "Error updating tset list. file "+tsetFileNames.at(i);
}
ReadXmlFile()非常大(并且涉及其他对象)但与创建列表相关的位是:
struct TDefVariant_t
{
QString name;
QWidget* nameWidget;
QList<TDefGroup_t *> groups;
TDefVariant_t();
~TDefVariant_t();
TDefVariant_t(const TDefVariant_t &other);
TDefVariant_t& operator=(const TDefVariant_t &rhs);
void CopyFrom(const TDefVariant_t &from);
void DeleteAllItems(); // deletes all items from the variant
} ; // Variant
TDefVaraint_t函数:
// first create a new variant
TDefVariant_t* variant = new TDefVariant_t;
m_variants.append(variant);
DeleteAllItems:
TDefVariant_t::TDefVariant_t()
{
}
TDefVariant_t::~TDefVariant_t()
{
this->DeleteAllItems();
}
TDefVariant_t::TDefVariant_t(const TDefVariant_t &other)
{
this->CopyFrom(other);
}
TDefVariant_t& TDefVariant_t::operator=(const TDefVariant_t &rhs)
{
if (this != &rhs) // only copy if they are not already the same object (ie at same address)
this->CopyFrom(rhs);
return *this;
}
void TDefVariant_t::CopyFrom(const TDefVariant_t &from)
{
name = from.name; // Variant top level
nameWidget = from.nameWidget;
this->DeleteAllItems(); // make sure item being copied to is cleanly cleared
for (int i = 0; i < from.groups.size(); i++) // for all groups....
{
TDefGroup_t *group = new TDefGroup_t; // create the new group.
groups.append(group);
group->name = from.groups.at(i)->name; // add the name
group->settings.clear();
for (int j = 0; j < from.groups.at(i)->settings.size(); j++) // for each setting
{
TDefSetting_t *setting = new TDefSetting_t; // create the new setting.
group->settings.append(setting);
//CopySetting(*(from.groups.at(i)->settings.at(j)), *setting); // now done by overloaded operator
*setting = *(from.groups.at(i)->settings.at(j));
}
}
}