-2

The settings: I'm trying to work with ImGUI to draw gui on windows created using SDL. I wrote a function called update in which it iterates over a forward_list of drawable objects and draws them, and then I call it in my main after polling events.

The code:

//vsc++11
__interface IDrawable { void draw(ImDrawList * draw_list, ImVec2 margin = ImVec2 (20,20)); }
class DrawableObj0 : public IDrawable { ...; public void draw (ImDrawList * draw_list, ImVec2 margin = ImVec2 (20,20)) { ...; }; ... }
void update(forward_list<IDrawable *> * drawables) { 
    ...;
    for (auto it = drawables->begin(); it != drawables->end(); ++it)
        (*it)->draw(...);    // Gets Access Violation Exception!
    ...;
}

void main(...) {
    forward_list<IDrawable *>* drawables = new forward_list<IDrawable *>();
    DrawableObj0** x = new DrawableObj0*[5];
    for (int i=0; i<5; i++){
        x[i] = new DrawableObj0(...);
        drawables->push_front(x[i]);
    }
    ...;
    update(drawables);
    ...;
}

The problem: As I mentioned in the code section, I get memory access violation 0xc while trying to access the draw function through the iterator. The visual studio shows that it can be resolved to the actual function in the watcher. I also tried it._Ptr->_myVal->draw but it has the same result and crashes at the same memory offset as the code above.

Edited the code thanks to @kfsone

Tala
  • 909
  • 10
  • 29
  • Did you verify if (*it) is null or not? Also, how many times does draw() get called before the crash? – joeking Apr 07 '16 at 23:22
  • @joeking in the watcher, *it is correctly referenced to the object pointer. It crashes on the first try. – Tala Apr 07 '16 at 23:25
  • Your list probably contains dangling pointers. There is a mistake somewhere in the code you replaced by `...` . You will need to post a [MCVE](http://stackoverflow.com/help/mcve) – M.M Apr 07 '16 at 23:27
  • When was `interface` added to the language? Is `draw` virtual? What's the difference between DrawableObj0 and DrawableObj? – kfsone Apr 07 '16 at 23:31
  • 1
    @kfsone `interface` is a MSVC extension: http://stackoverflow.com/questions/25234203/what-is-the-interface-keyword-in-msvc – derekerdmann Apr 07 '16 at 23:32
  • @kfsone it is actually `__interface` in the code and it works with VS c++ compiler. `DrawableObj` is a typo, I meant `DrawableObj0`. `draw` is not virtual, it's an interface which in some sense is virtual in practice, but has different concepts behind. – Tala Apr 07 '16 at 23:41
  • It seems the fact that `IDrawable` is an interface messes things up. I tried another version avoiding pointers as much as I can and got the compiler error: `C3153 'IDrawable': you cannot create an instance of an interface ImmDBG C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\forward_list 376` – Tala Apr 08 '16 at 00:10

1 Answers1

1

Thank you all for your participation.

It seems that though my method signatures were the same in both interface and implementations, having defaults for parameters leads to messing things up. The variable of the implemented class type, is considered the interface itself and at the runtime (*it)->draw() looks for the method reference in memory as if it is an instance of IDrawable not the DrawableObj0.

Tala
  • 909
  • 10
  • 29