CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

autoPtr, derived classes and memory leak

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   July 28, 2009, 18:50
Default autoPtr, derived classes and memory leak
  #1
Senior Member
 
John Deas
Join Date: Mar 2009
Posts: 160
Rep Power: 17
johndeas is on a distinguished road
Hi,

i am trying to understand generic programming concepts used in OpenFOAM, and have a basic question in C++.

suppose I have a "employee" base class which has virtual functions, and two derived classes:
Code:
employeeExportA:public employee
and
Code:
employeeExportB:public employee
I want to have a pointer to the employee base class called myEmployee, and be able to choose at runtime wether it points to a employeeExportA or employeeExportB object. I tried this code:

Code:
    auto_ptr<employee> myEmployee;
    if (inputString == "A")
    {
         myEmployee = auto_ptr<employeeExportA>(new employeeExportA(4,5));
    if (inputString == "B")
    {
         myEmployee = auto_ptr<employeeExportB>(new employeeExportB(4,5));;
    }
My program compiles, but I would like to know, since the auto_ptr myEmployee has been created with the base class, will it be able to clean all the memory created by the "new" operator applied to a derived class, or will it only clean the part of the object in common with the base and let the remaining leaking ?
johndeas is offline   Reply With Quote

Old   July 29, 2009, 06:29
Default
  #2
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by johndeas View Post
Hi,

i am trying to understand generic programming concepts used in OpenFOAM, and have a basic question in C++.

suppose I have a "employee" base class which has virtual functions, and two derived classes:
Code:
employeeExportA:public employee
and
Code:
employeeExportB:public employee
I want to have a pointer to the employee base class called myEmployee, and be able to choose at runtime wether it points to a employeeExportA or employeeExportB object. I tried this code:

Code:
    auto_ptr<employee> myEmployee;
    if (inputString == "A")
    {
         myEmployee = auto_ptr<employeeExportA>(new employeeExportA(4,5));
    if (inputString == "B")
    {
         myEmployee = auto_ptr<employeeExportB>(new employeeExportB(4,5));;
    }
My program compiles, but I would like to know, since the auto_ptr myEmployee has been created with the base class, will it be able to clean all the memory created by the "new" operator applied to a derived class, or will it only clean the part of the object in common with the base and let the remaining leaking ?
That depends. Google for "virtual destructor"
gschaider is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:03.