Pure virtual function call error

Source: Internet
Author: User

I do not know why there is no problem in debugging when I am working on the cximage library in the internship unit, but a pure virtual function call error exists in the release version! What happened? Let's find out what happened!

Description on msdn

Http://forums.msdn.microsoft.com/zh-CN/clr/thread/bfa759a8-0cc0-4d65-8711-d2d0d6765687/

 

This is detailed.

Http://www.artima.com/cppsource/pure_virtual.html

 

Let's look at an example on codeproject.

Http://www.codeproject.com/KB/cpp/PureVirtualFunctionCall.aspx

 

(1) The reason for pure virtual function call is:

The child destructor is called and finished, so, we don't have an object of type child now, but the current object (which is only being destructed) is of Type parent, so all callin the Destructor are callto functions in this object. so, you can get a pure virtual function.

The destructor of the inherited class is called and released, so this object does not exist now, but the current object (the parent class object being interpreted) but you need to call the virtual function implementation of its subclass (because it is a pure virtual function), so pure virtual function call error will occur!

 

(2) It is known from the object-oriented concept that the object's destructor are sub-classes first and then to the parent class's destructor, therefore, it is dangerous to call its own virtual pure number in the parent class, and it appears at runtime.

 

The following is an example of codeproject:

 

Class Parent {public: parent (){}~ Parent () {clearall () ;}void clearall () {thepure (); // call your own pure virtual function, the reason for packaging is that calling the compiler directly will identify a problem !} Virtual bool thepure () = 0 ;}; class child: Public parent {public: Child (){}~ Child () {}// The Implementation of the pure virtual function virtual bool thepure () {return true ;}}; void main () {Child C ;}

When the C destructor is created, the thepure () in the virtual function table has been canceled, so the thepure () in clearall () in the parent class () the virtual table Pointer Points to an empty address (which becomes the dangling pointer), So calling clearall () in the parent will cause an error.

 

(3) If the above situation is still intuitive, let's look at the following situation that causes pure virtual function call to be more concealed.

// However, I have no problem in vc6. class base {public: Virtual void function () = 0 ;}; Class B: public base {public: Virtual void function () {cout <"pure function call in B" <Endl ;}}; Class A {public: A (B * B): _ B (B ){}~ A () {_ B-> function ();} B * _ B;}; B; A (& B); int main () {return 0 ;}

 

In some compilers, a pure virtual function call error occurs. The main reason is that the release sequence of global variables is uncertain, and global variable a depends on global variable B. If the compiler decides to release B first, then release. Then, when a is destructed, _ B is a dangling pointer. If the Memory System of object B is not released, the vptr table of object B points to the base vptr instead of B. In this case, _ B-> function () points to the pure virtual function. For details, refer

http://www.cnblogs.com/whjiang/archive/2007/10/22/932880.html

(5) summary on pure virtual function call on the Forum:

 

1. The base class constructor calls the virtual function directly.

2. The basic destructor calls the virtual function directly.

3. The base class constructor indirectly calls virtual functions.

4. The basic destructor indirectly calls virtual functions.

5. Calling a virtual function via a dangling pointer.

(4) In case of pure virtual function call, a pure virtual function in a base class can be implemented, but remember to delete the implementation code after finding out the cause.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.