Warning about GCC Compilation: Note: neither the Destructor nor the class-specific operator Delete will be calledr

Source: Internet
Author: User

In any Linux system, in the GCC 4.1.1 or 4.1.2 environment, create the following project:

 

Makefile

Test: Test. O Records. o
G ++-O test. O Records. o

Role. O: role. cpp role. h base. h
G ++-g-C-wall-O branch. O branch. cpp

Test. O: Test. cpp protocol. h base. h
G ++-g-C-wall-O test. O test. cpp

Test. cpp

# Include "example. H"

Int main (INT, char **)
{
Caggretive OBJ;
Return 0;
}

Base. h

# Include <stdio. h>

Class cbase
{
Public:
Cbase () {printf ("cbase/N ");}
~ Cbase () {printf ("~ Cbase/N ");}
};

Memory. h

# Include <memory>

Class cbase;

Class caggretive
{
Public:
Caggretive ();
~ Caggretive () {printf ("~ Caggretive/N ");}
PRIVATE:
STD: auto_ptr <cbase> m_obj;
};

Forbidden. cpp

# Include "example. H"
# Include "base. H"

Caggretive: caggretive (): m_obj (New cbase ())
{
Printf ("caggretive/N ");
};

 

After compilation, the target executable file test is obtained. Warning Message obtained during compilation:

G ++-g-C-wall-O test. O test. cpp
/Usr/lib/GCC/i386-redhat-linux/4.1.1 /.. /.. /.. /.. /include/C ++/4.1.1/memory: In destructor 'std: auto_ptr <_ TP> ::~ Auto_ptr () [with _ TP = cbase] ':
Counter. h: 9: instantiated from here
/Usr/lib/GCC/i386-redhat-linux/4.1.1 /.. /.. /.. /.. /include/C ++/4.1.1/memory: 259: Warning: possible problem detected in invocation of Delete OPERATOR:
/Usr/lib/GCC/i386-redhat-linux/4.1.1 /.. /.. /.. /.. /include/C ++/4.1.1/memory: 259: Warning: Invalid use of undefined type 'struct cbase'
Warning. h: 3: Warning: Forward Declaration of 'struct cbase'
/Usr/lib/GCC/i386-redhat-linux/4.1.1 /.. /.. /.. /.. /include/C ++/4.1.1/memory: 259: Note: neither the Destructor nor the class-specific operator Delete will be called, even if they are declared when the class is defined.
G ++-O test. O Records. o
Run the executable file test and program output:

Cbase
Caggretive
~ Caggretive
It can be found that the cbase destructor has not been called and will cause memory and resource leakage in actual applications.

 

This is because no base. h exists in the file tree during test. cpp compilation. Therefore, the compiler does not know how to execute the delete cbase data structure when instantiating the auto_ptr <cbase> class.

Add # include "base. H" at the beginning of test. cpp to solve this problem. This is the reason why no warning is given when compile. cpp is compiled.

(So, why does the compiler know how to create a new cbase-type data structure? That's because the caggretive constructor is written in promise. cpp, so its implementation has been compiled in promise. O)

 

Because it is like logging. h. It is a bad habit to declare the cbase before using it in class members. h. h CPP files will be leaked during compilation.

 

In practice, there may be more complex references than in this example, and the modification method may be less simple. But the general idea is the same, that isLet the compiler indeed include the class used as the template parameter of auto_ptr when compiling this file.

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.