I have a function pointer void (*Draw)(WidgetVisualization *); in class WidgetVisualization, in WidgetVisualization.h.
I’ve got extern void GraphicVisualizationDraw(WidgetVisualization *w) in WidgetVisualization.cpp.
In WidgetVisualization’s constructor, I’m assigning Draw = GraphicVisualizationDraw;.
And here is the function from LCDGraphic.cpp:
void GraphicVisualizationDraw(WidgetVisualization *widget) {
}
When I try to assign the function pointer ‘Draw’ to the function GraphicVisualizationDraw, I get an invalid write. Any clue?
Here’s the output from 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;
This error does not seem to do with the code of the class itself, it seems to be something related with the memory position in which the object of the
WidgetVisualizationclass is created.The problem is that when the initialization of Draw is carried out, is when the runtime realises that the object is in an incorrect position.
Following the stack trace:
A member function called
BuildLayouts()seems to be a good candidate to be the one that is doing a bad allocation of this object. You should double-check that code.