Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Pure virtual function called


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Mar 22, 2018 7:42 AM Last Post By: Niki Minkov
Jeff Allan

Posts: 6
Registered: 5/24/01
Pure virtual function called  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 8, 2017 12:39 PM
My app is displaying the error message, "Pure virtual function called".
The error seems to be non-deterministic, in that I cannot generate it by repeating a fixed set of steps.
The error message doesn't tell me what function or class is involved.
When I stop the IDE it does not display a call stack.

How can I determine which class/function is being called?

Thank you,
Jeff Allan

My devel environment is: ECB XE, using VCL, Win 7 x64
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Pure virtual function called  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 8, 2017 2:41 PM   in response to: Jeff Allan in response to: Jeff Allan
Jeff wrote:

My app is displaying the error message, "Pure virtual function called".

That means either:

1. a derived class is calling a method of its base class in a constructor
or destructor, and that called method is then calling a method that is abstract
in the base class (regardless of whether the derived class overrides it).

2. a Delphi-based abstract class is being directly instantiated in Delphi
code (C++ does not allow that, but Delphi does) and then an abstract method
is called on that object.

The error seems to be non-deterministic, in that I cannot generate it
by repeating a fixed set of steps.

The error message doesn't tell me what function or class is involved.

When I stop the IDE it does not display a call stack.

Then you have no hope of tracking it down, unless you use trial-and-error
to narrow down the code that is throwing the error.

--
Remy Lebeau (TeamB)
Niki Minkov

Posts: 20
Registered: 3/3/10
Re: Pure virtual function called  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 20, 2018 4:01 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Is there a int __cdecl _purecall(void) or _set_purecall_handler() way for MSVC to handle pure virtual in BCB?

Remy Lebeau (TeamB) wrote:
Jeff wrote:

My app is displaying the error message, "Pure virtual function called".

That means either:

1. a derived class is calling a method of its base class in a constructor
or destructor, and that called method is then calling a method that is abstract
in the base class (regardless of whether the derived class overrides it).

2. a Delphi-based abstract class is being directly instantiated in Delphi
code (C++ does not allow that, but Delphi does) and then an abstract method
is called on that object.

The error seems to be non-deterministic, in that I cannot generate it
by repeating a fixed set of steps.

The error message doesn't tell me what function or class is involved.

When I stop the IDE it does not display a call stack.

Then you have no hope of tracking it down, unless you use trial-and-error
to narrow down the code that is throwing the error.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Pure virtual function called  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 20, 2018 5:17 PM   in response to: Niki Minkov in response to: Niki Minkov
Niki Minkov wrote:

Is there a int __cdecl _purecall(void) or _set_purecall_handler()
way for MSVC to handle pure virtual in BCB?

Kind of.

There is an AbstractErrorProc function pointer declared in System.hpp
that you can assign a custom function to. However, it doesn't seem to
be called when a pure abstract method is called in C++ (probably only
in Delphi).

After some further research, I see that in C++, the compiler uses a
_pure_error_() handler in pureerr.cpp, which calls an _ErrorExit()
function in errmsg.c. By default, _ErrorExit() displays the specified
error message in a console window or a popup MessageBox, depending on
project type. However, there is a _messagefunc callback function
pointer and a _messagefile char* variable that you can assign to. If
_messagefunc is assigned, your callback function will be called with
the error message string as an input parameter. If _messagefile is
assigned a filename, the error message will be written to the specified
file.

However, _ErrorExit() gets called for a lot of different kinds of RTL
errors, so you would have to look at the actual error message to know
whether it is a pure abstract error or not.

For example:

class TBase
{
public:
    TBase() { DoTest(); }
    virtual void DoTest() = 0;
};
 
class TDerived : public TBase
{
public:
    TDerived() : TBase() {}
    virtual void DoTest() {}
};
 
...
 
extern "C" int (*_messagefunc) (char *msg);
 
int MyErrorMsgFunc(char *msg)
{
    if (strcmp(msg, "Pure virtual function called") == 0)
    {
        // ...
    }
    return 0; // <-- return value is ignored!
}
 
_messagefunc = &MyErrorMsgFunc;
TDerived d;


--
Remy Lebeau (TeamB)
Niki Minkov

Posts: 20
Registered: 3/3/10
Re: Pure virtual function called  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 22, 2018 7:42 AM   in response to: Niki Minkov in response to: Niki Minkov
Got it!

C++ Builder handler is _pure_error_(). All that you need is to implement:

extern "C" void _RTLENTRY _pure_error_()
{
    //_ErrorExit("Pure virtual function called");
    throw Exception("Pure virtual function called");
}


and check/log the call stack when the pure virtual auccures
Arkady Semylio

Posts: 9
Registered: 5/25/17
Re: Pure virtual function called  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 21, 2018 4:04 PM   in response to: Jeff Allan in response to: Jeff Allan
Jeff Allan wrote:
My app is displaying the error message, "Pure virtual function called".
The error seems to be non-deterministic, in that I cannot generate it by repeating a fixed set of steps.
The error message doesn't tell me what function or class is involved.
When I stop the IDE it does not display a call stack.

How can I determine which class/function is being called?

Thank you,
Jeff Allan

My devel environment is: ECB XE, using VCL, Win 7 x64

Usually this means that you are calling a virtual method from a constructor of an abstract
class (directly or indirectly).
That is, I mean that you are probably calling a pure virtual method in the context of a constructor.
This can happen because C ++ constructs the objects starting from the bases gradually up
to the derived classes. If a constructor calls a pure virtual method, no related (implemented)
method of a concrete derived class can be invoked, as that class doesn't yet exist.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02