-1

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

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

在 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;
4

1 回答 1

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()似乎是对该对象进行错误分配的一个很好的候选者。您应该仔细检查该代码。

于 2012-05-04T11:00:19.800 回答