how to use the virtual destructor?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugarva
    New Member
    • Apr 2008
    • 10

    how to use the virtual destructor?

    Hi all,
    I have a piece of code like this
    constructor:

    IPCClientDataTr ansfer::IPCClie ntDataTransfer (RWCString serviceName) :Valid(FALSE)
    {
    NSTcpProfile tcpProfile;
    int clientPriority = 1;
    tcpProfile.Bloc kingOnConnect = TRUE;
    tcpProfile.Bloc king = TRUE;
    strcpy(tcpProfi le.ServiceName, serviceName);
    strcpy(tcpProfi le.TcpHostName, "localhost" );

    this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority) ;

    // Check to see if the IPCAMAClient is a valid object
    if (!this -> PClient -> isValid())
    {
    // The object is invalid, the connection was not established
    delete this -> PClient;
    this -> PClient = NULL;
    }
    else
    {
    // The Object is valid
    this -> Valid = TRUE;
    }
    destructor:

    IPCClientDataTr ansfer::~IPCCli entDataTransfer ()
    {
    if (this -> PClient)
    delete this -> PClient;
    }

    the destructor is not virtual.does this means that there will be memory leak?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There will be no leak in the IPCClientDataTr ansfer class.

    However, if IPCClientDataTr ansfer has virtual functions and another class derives from IPCClientDataTr ansfer, you may have a leak.

    In this case the derived class object is identified by either a base class reference or a base class pointer (simple C++ polymorphism). Without the virtual destructor in IPCClientDataTr ansfer, the destructor of the derived class will not be called when the pointer or reference to IPCClientDataTr ansfer is deleted.

    Comment

    • sugarva
      New Member
      • Apr 2008
      • 10

      #3
      ok.thanks a lot.
      there are no virtual functions in IPCClientDataTr ansfer.C class.
      in this line
      this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority) ;

      a new instance has been created for IPCAMAClient class.
      but i dont think it is a derived class of IPCClientDataTr ansfer.C class.

      whether still the memory leak problem exist because the IPCClientDataTr ansfer.C class destructor is not virtual?

      Comment

      • sugarva
        New Member
        • Apr 2008
        • 10

        #4
        and one more info

        IPCClientDataTr ansfer is wrapper class around IPCAMAClient.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          From what I see the new IPCAMAClient object allocated in the IPCClientDataTr ansfer constructor will be deleted by the IPCAMAClient destructor. So you shouldn't leak.

          I did notice that in one place you:
          Originally posted by sugarva
          // The object is invalid, the connection was not established
          delete this -> PClient;
          this -> PClient = NULL;
          }
          but in ther destructor you:
          Originally posted by sugarva
          if (this -> PClient)
          delete this -> PClient;
          It is not required to test for zero before deleting but it is important to zero after deleting in case you delete again. Deleting twice will crash the program.

          You do not zero in the destructor. Somebody could call the destructor more than once and your prgram would crash.

          In both places I would change to:
          [code=cpp]
          delete this->PClient;
          this->PClient = 0;
          [/code]

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            I was under the impresion that a virtual destructor is illegal.Is this wrong?

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              Yes. Virtual constructors are illegal, but virtual destructors are not only legal but also very useful to deal with the inherent base class object created with derived class objects.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                Thanks......i need to re-read my books and do some more exercises on connstructors and destructors.

                Comment

                • sugarva
                  New Member
                  • Apr 2008
                  • 10

                  #9
                  thaks a lot.

                  you have said that IPCAMAClient destructor will delete the object.

                  but the destructor is
                  /*************** *************** *************** *************** ****
                  Method: ~IPCAMAClient

                  Description: The destructor. Does nothing

                  Parameters: None

                  Returns: void
                  *************** *************** *************** *************** ****/
                  IPCAMAClient::~ IPCAMAClient( )
                  {
                  }

                  /*************** *************** *************** *************** ****


                  it does nothing.
                  is it necessary that i should delete the object here and to zero that?

                  whether deleting the objects in two classes is necessary?

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by sugarva
                    it does nothing.
                    is it necessary that i should delete the object here and to zero that?

                    whether deleting the objects in two classes is necessary?
                    Remember, in C++ constructors do not create objects and destructors do not delete objects.

                    Only the compiler can create and delete objects. As part of that process, when the object is created the compiler, as a courtesy, will call your constructor so you can initialize the already created object. Similarly, when the object is being deleted the compiler, again as a courtesy, will call your destructor so you can release any memory allocations you may have made.

                    Constructors and destructors are just functions like any other.

                    If you happen to call your destructor yourself, then any variables you allocated in the object will be released but your object is still there. Calling a destructor is a convenient way to release the current contents of an object when you intend to install new contents, like with an assignment operator overload.

                    If your class contains no variables allocated using the new operator, then you don't need a destructor at all.

                    Comment

                    • sugarva
                      New Member
                      • Apr 2008
                      • 10

                      #11
                      ok.thanks.
                      so,in your opinion there won't be any memory leak.right?

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Again, if you do not use the new operator, there can never be a leak.

                        If you so use the new operator and your delete is in your destructor, then there is no leak.

                        If you use the new operator more than once without deleting the previous allocationm, then you will have a leak.

                        Comment

                        Working...