1

I have a very odd Problem:

I use one of my base classes: IEventlistener() which gets implemented by many other classes. Most of the time the system works. But now a very strange problem occured.

Once class (CGUIService) implements the interface, one method (VGetListenerName) works as expected the other one (VHandleMessage) gets a pure call when I check the callstack and I don't understand why -.- (seems as if the vtable gets somehow overwritten or out of bounds ...)

I made a screenshot so you can see variable before I call the VHandleMessage, which of course leads to a debug assertion R6025 - pure virtual function call because somehow the implemented method isn't entered in the vtable of the IEventlistener().

Highres: www.fantasyhaze.com/cb/Error_purecall.png www.fantasyhaze.com/cb/Error_purecall.png

I hope someone can give me a hint :)

Edit1.)

So just to explain it a little bit more I have created a new screenshot which shows the same process, but now I have included 2 more virtual function which are not pure and have a implementation (becuase of time reasons, I have not the time to implement all methods again in each class which uses the interface)

The purple ones are the new ones, The red one is the one which does not work The orange one is the method which was there before and which worked and still works

You can see on the left side, that the VGetListenerName,VHandleEvent1,VHandleEvent2 work (debugpoint + current position) and that those 3 are in the vtable ... but not the important one (red)

highres: www.fantasyhaze.com/cb/Error_purecall2.png vtable problem 2

Edit2.)

SOLUTION:

The main problem was, that CGUIService inherites from IBase. To have access to the Service I used a Service Locator which stores each service. Therefore it performes a static_cast in the Instance Getters Service::GetServiceInstance() and a static_cast was also performed to store the service as IBase. But IEventListener was not implemented in IBase furthermore the Service was cast back to IBase witout IEventListener and the vtable wasn't ok. Now IBase implements IEventListener and it works because the static_cast casts IEventListener correctly :)

Thx for the hints guys :)

  • 1
    Answers in http://stackoverflow.com/questions/4612702/what-can-cause-a-pure-virtual-function-call-in-c seem to discuss quite completely how can a pure virtual call happen. – Suma Jan 26 '11 at 08:27
  • THX there was a hint about casting, this was the problem :) –  Jan 26 '11 at 11:17

3 Answers3

2

Often pure virtual call is caused by calling a virtual function on an already destroyed object. When an object is being destroyed every base class destructor sets the virtual table pointer to class's virtual table. If a function is pure virtual and does not have implementation, pure virtual call handler address is put in the virtial table.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • The object is not destroyed because all other function work just one does not work The VHandleEvent has a implementation in GUIService.cpp (you see the GUIService.h and the header) –  Jan 26 '11 at 08:54
  • All other functions work does not mean it has not been destroyed. Put a breakpoint in the destructor `__asm int 3;` or something to make sure the object is not destroyed prematurely. – Maxim Egorushkin Jan 26 '11 at 09:57
  • no problem with destruction. But I found a solution already :) –  Jan 26 '11 at 11:15
1

It is hard to judge from one screenshot, but from the information you gave my first guess would be a dangling pointer - perhaps you attempt to call the virtual method on an object which was already destroyed? Another possibility is the object is not completely constructed yet, but this is not very likely, as this would most likely needs some race condition in multiple threads.

This and other possibilities are listed in What can cause a pure virtual function call in C++? quite exhaustively.

Community
  • 1
  • 1
Suma
  • 33,181
  • 16
  • 123
  • 191
  • THX for the link I'll double check it. and no it isn't a dangling pointer see answer @ Maxim Yegorushkin I have only one thread running in windows, and the object is fully constructed! I have already checked that a while ago just to be sure. –  Jan 26 '11 at 08:55
0

purecall means that for whatever reason the class implementing an interface has less virtual functions than the interface it implements. This often happens when:

  • you add a method into an interface
  • use the new method in the caller
  • recompile the caller, but don't recompile the implementaion (or recompile the implementation and forget to re-link against it)

So it's likely a build process problem. Check that you recompile all your stuff the caller depends on and if necessary re-link the caller.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • I have recompiled already a dozen times I guess it's no linking problem –  Jan 26 '11 at 08:55