472,779 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

Pure Virtual Function Calls

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
6 3350
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: IK | last post by:
Hello All, Please excuse me for posting this here, but I don't find any other group where I will get a proper answer. This is about clarifying the C++ part of COM. I understand that COM is a...
2
by: Derek | last post by:
The following simple program causes a runtime error (pure virtual method called): struct Base { Base() { init(); } void init() { badIdea(); } virtual void badIdea() = 0; };
5
by: Ruben Campos | last post by:
I've recently noticed that it's not allowed to call a pure (non-implemented) virtual method inside a constructor or a destructor, doesn't matter if this method is declared in the considered class...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.