473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3432
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.