By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

Pure Virtual Function Calls

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

--
Martin Richter [MVP] WWJD
"In C we had to code our own bugs. In C++ we can inherit them."
FAQ : http://www.mpdvc.de
Samples: http://www.codeguru.com http://www.codeproject.com
Nov 17 '05 #2

P: n/a
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?

Nov 17 '05 #3

P: n/a
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/de...ll_handler.asp

Regards,
Oleg
[VC++ MVP]


Nov 17 '05 #4

P: n/a
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.
"Martin Richter [MVP]" wrote:
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.

--
Martin Richter [MVP] WWJD
"In C we had to code our own bugs. In C++ we can inherit them."
FAQ : http://www.mpdvc.de
Samples: http://www.codeguru.com http://www.codeproject.com

Nov 17 '05 #5

P: n/a
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


Nov 17 '05 #6

P: n/a
pakis wrote:
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
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.