我在一个最近没有太大变化的类的构造函数中遇到了奇怪的崩溃。我已经剪掉了很多东西,但这里是它的要点:
#define Accessor(PropName, Type, Default) \
Type PropName() { Type *Member = (Type*)Props.Find(Prop##PropName); \
if (Member) return *Member; \
return Default; } \
void PropName(Type t) { Type *Member = (Type*)Props.Find(Prop##PropName); \
if (Member) *Member = t; \
else { Props.Add(Prop##PropName, Member = new Type); \
*Member = t; } \
OnChange(Prop##PropName); }
class GCss
{
GHashTbl<int, void*> Props;
public:
virtual ~GCss() {}
virtual void OnChange(PropType p) {}
Accessor(TextAlign, Len, Len()); // and lots of others obviously
};
class GDom : virtual public GDomI
{
public:
virtual ~GDom() {}
virtual bool GetVariant(const char *Name, GVariant &Value, char *Array = 0) { return false; }
virtual bool SetVariant(const char *Name, GVariant &Value, char *Array = 0) { return false; }
};
class GLayoutCell : public GDom, public GCss
{
public:
};
class TableCell : public GLayoutCell
{
public:
TableCell(GTableLayout *t, int Cx, int Cy)
{
TextAlign(AlignLeft); // this call crashes trying to call 'OnChange'
}
};
vtable 中的 OnChange 方法指针似乎为 NULL。现在我做了很多事情来尝试检查我没有做一些愚蠢的事情。使用相当默认的项目设置在 XCode 4.5 中从头开始重建代码,还尝试了 XCode 3.x,结果相似。我试过从 GLayoutCell 继承一个测试对象并且没有崩溃。崩溃本身位于一个名为 Lgi 的私有框架中。它是开源的,所以你可以在这里查看:
- GDom.h
- GTableLayout.h , GTableLayout.cpp
- GCss.h , GCss.cpp
我现在很困。我不知道下一步该尝试什么。哦,堆栈看起来像:
#0 0x00341880 in typeinfo for GDom ()
#1 0x00252698 in TableCell::TableCell(GTableLayout*, int, int) at /Users/matthew/Code/Lgi/trunk/src/mac/carbon/../../common/Widgets/GTableLayout.cpp:394
#2 0x0024d9cb in GTableLayout::GetCell(int, int, bool, int, int) at /Users/matthew/Code/Lgi/trunk/src/mac/carbon/../../common/Widgets/GTableLayout.cpp:1562
#3 0x0023f8eb in GProgressPane::GProgressPane() at /Users/matthew/Code/Lgi/trunk/src/mac/carbon/../../common/Widgets/GProgressDlg.cpp:129
#4 0x00241ff8 in GProgressDlg::Push() at /Users/matthew/Code/Lgi/trunk/src/mac/carbon/../../common/Widgets/GProgressDlg.cpp:407
#5 0x00241fb8 in GProgressDlg::OnCreate() at /Users/matthew/Code/Lgi/trunk/src/mac/carbon/../../common/Widgets/GProgressDlg.cpp:366
好像是在对 GDom 类做点什么,其实和 GCss 当前类没有任何关系。对我来说闻起来像一个编译器错误,但我不想下结论。顺便说一句,Valgrind 没有发现任何损坏。