也许有人可以看到我缺少的东西(调试案例,C ++,函数指针)

时间:2012-05-04 10:47:09

标签: c++ debugging

我在WidgetVisualization.h中的WidgetVisualization类中有一个函数指针void (*Draw)(WidgetVisualization *);

我在WidgetVisualization.cpp中有extern void GraphicVisualizationDraw(WidgetVisualization *w)

在WidgetVisualization的构造函数中,我正在分配Draw = GraphicVisualizationDraw;

这是LCDGraphic.cpp的功能:

void GraphicVisualizationDraw(WidgetVisualization *widget) {

}

当我尝试将函数指针'Draw'赋值给函数GraphicVisualizationDraw时,我得到一个无效的写。任何线索?

这是valgrind的输出:

==8281== Invalid write of size 4
==8281==    at 0x52D12C7: LCD::WidgetVisualization::WidgetVisualization(LCD::LCDCore*, std::string, Json::Value*, int, int, int) (WidgetVisualization.cpp:72)
==8281==    by 0x530F0C3: LCD::LCDCore::BuildLayouts() (LCDCore.cpp:342)
==8281==    by 0x5306A07: LCD::LCDControl::ConfigSetup() (LCDControl.cpp:213)
==8281==    by 0x53062EC: LCD::LCDControl::Start() (LCDControl.cpp:57)
==8281==    by 0x52D84E4: (anonymous namespace)::lcdcontrol_init(_VisPluginData*) (actor_lcdcontrol.cpp:115)
==8281==    by 0x40645FA: visual_plugin_realize (lv_plugin.cpp:207)
==8281==    by 0x405B1E7: visual_actor_realize (lv_actor.cpp:265)
==8281==    by 0x404C02B: visual_bin_realize (lv_bin.c:108)
==8281==    by 0x80507D1: main (lv-tool.cpp:378)


WidgetVisualization::WidgetVisualization(LCDCore *v, std::string n, Json::Value *section, int row, int col, int layer) : Widget(v, n, section, row, col, layer,
    WIDGET_TYPE_VISUALIZATION | WIDGET_TYPE_RC | WIDGET_TYPE_SPECIAL) {

    LCDError("widgetvisualization: layer: %d", layer_);

    if(lcd_type_ == LCD_TEXT)
        Draw = TextVisualizationDraw;
    else if(lcd_type_ == LCD_GRAPHIC)
        Draw = GraphicVisualizationDraw;// line 72
    else
        Draw = 0;

1 个答案:

答案 0 :(得分:0)

这个错误似乎与类本身的代码无关,它似乎与创建WidgetVisualization类的对象的内存位置有关。

问题在于,当执行Draw的初始化时,是运行时意识到对象处于不正确的位置。

跟踪堆栈跟踪:

==8281== Invalid write of size 4
==8281==    at 0x52D12C7: LCD::WidgetVisualization::WidgetVisualization(LCD::LCDCore*, std::string, Json::Value*, int, int, int) (WidgetVisualization.cpp:72)
==8281==    by 0x530F0C3: LCD::LCDCore::BuildLayouts() (LCDCore.cpp:342)

一个名为BuildLayouts()的成员函数似乎是一个很好的候选者,可以成为对这个对象进行错误分配的人。你应该仔细检查那段代码。