Pure Virtual Function Calls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having a problem of pure virtual function call in my project.

Can anyone explaine me the causes of pure virtual function calls other than
calling a virtual function in base class?

Thanks
 
Hallo pakis!
I am having a problem of pure virtual function call in my project.

What problems?
Can anyone explaine me the causes of pure virtual function calls other than
calling a virtual function in base class?

A normal call to a member or static function of a class can be directly
resolved by the compiler and linker. If the this pointer is invalid. the
correct function will start to execute but the function may crash when
data via the this pointer is accessed.

A virtual function uses a vtable (a table of pointers to the virtual
functions), so when calling a virtual function the this pointer must be
correct because the code that executes a virtual function will fetch the
address for the function at the proper index of the vtable.

So calling a non-virtual function with the object pointer==NULL is
possible, but calling a virtual-function with a NULL pointer usually
causes a crash.

I.e.
CWnd *pWnd = NULL;
pWnd->GetSafeHwnd();
will return FALSE;
The function checks if the this pointer is false.
Calling any virtual function of CWnd will immediately crash.
 
The most common cause for pure virtual function calls are build
inconsistencies. Normally, a pure virtual function can never be called,
since the compiler won't let you create an instance of a class which doesn't
provide an implementation for a pure virtual function. So the first thing to
try when you get that message is a "rebuild all".
 
I am having a problem of pure virtual function call in my project.

Can anyone explaine me the causes of pure virtual function calls other than
calling a virtual function in base class?

Sometimes it can happen if there is a problem in destruction logic of a class hierarchy
(when a base class that declared the virtual function as pure calls that same function
from its destructor; usually "with the help" of some "cleanup" functions)

Usually you can identify the reason of the problem by breaking into debugger
and looking at the call stack. Then you will see exactly who is trying to call
the pure virtual function.

In debug builds, you can rely on the assertion that is displayed by CRT library
when it encounters a pure virtual function call (use "Retry" to break into debugger).
Alternatively, you can set a breakpoint in purecall() function (enter "__purecall"
in New Breakpoint window) and wait until it is hit.

One more alternative is to register a custom purecall handler function
and use it to break into debugger:
http://msdn.microsoft.com/library/d...n-us/vclib/html/_crt_set_purecall_handler.asp

Regards,
Oleg
[VC++ MVP]
 
Actually i get runtime virtual function call exception.
Scenario is that i have a class Y which is derived from X. Class X has pure
virtual function a() which is implemented in class Y. I have a STL map in
some other class T in which i store pointer to Y against some value. In some
particular scenario, when i fetch pointer to class Y from map and call a(),
pure virtual function call occurs. Moreover, pointer is not NULL.

I delete the pointer from map in destructor of X.

Moreover, this is a multithreaded environment.
 
Actually i get runtime virtual function call exception.
Scenario is that i have a class Y which is derived from X. Class X has pure
virtual function a() which is implemented in class Y. I have a STL map in
some other class T in which i store pointer to Y against some value. In some
particular scenario, when i fetch pointer to class Y from map and call a(),
pure virtual function call occurs. Moreover, pointer is not NULL.

I delete the pointer from map in destructor of X.

Moreover, this is a multithreaded environment.

It is possible that the map contains a pointer to an already destroyed object.
When you delete objects from the map, check that the object has been found
in the map, and the deletion succeeded.

If the application is multithreaded, make sure that other threads do not access
the map when X destructor is running.

I would also recommend to check for heap corruption (e.g. with PageHeap),
just in case.

Oleg
 
pakis said:
I am having a problem of pure virtual function call in my project.

Can anyone explaine me the causes of pure virtual function calls
other than calling a virtual function in base class?

There are about 2 ways to get a pure virtual function call.

1. Calling a virtual function from the constructor or destructor of a class
where that virtual function is declared pure. Unlike C# or Java, C++ will
NOT call a virtual function overridden in a derived class when called from
the constructor or destructor of a base class.

<code>

stuct A
{
virtual void f() = 0;

A() { f(); }
~A() { f(); }
};

struct B : A
{
};

int main()
{
B b; // results in two pure calls
}

</code>

2. Calling a virtual function on a pointer to a deleted object where the
function was declared pure in the base class. This is actually undefined
behavior in C++, but under VC++ it will result in a pure virtual function
call if the function was in fact declared pure in the base class.

<code>

stuct A
{
virtual void f() = 0;
};

struct B : A
{
void f() {}
};

int main()
{
A* pa = new B();
delete pa;
pa->f();
}

</code>

Note that there are more complex variants of both of these cases, but
they'll boil down to one of these two.

-cd
 
Back
Top