Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:54:25+00:00 2026-06-10T19:54:25+00:00

A mate of mine told me, I’ve a memory leak in this code Base

  • 0

A mate of mine told me, I’ve a memory leak in this code

Base
{
    public:
        vector<Foo*> fooes;
};

Derived : public Base
{
    public:

        Derived ( )
        {
            for ( int i = 0 ; i < 10 ; i++ )
            {
                    this.fooes.push_back ( new Foo() );
            }
        };

};

But he is a very busy man and he can not help me, so I ask you, where is the memory leak? And how do I fix it?
As I understand it, the memory leak is that I do not delete objects, created by new Foo(), so I just can add a destructor to Base, and clear fooes vector, right?

Base
{
    public:
        vector<Foo*> fooes;
        ~Base ( )
        {
            this->fooes.clear();
        };

};

The question is:

  1. Is this a correct memory leak fix?

  2. Will the destructor of Base be called before the destructor of Derived, or not?

  3. Will the fooes vertor be deleted automatically while deleting Base or I must delete all members of the class manually?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:54:26+00:00Added an answer on June 10, 2026 at 7:54 pm

    1) Is this a correct memory leak fix?

    No, you have to iterate through the elements and manually delete them.

    2) Will the destructor of Base be called before the destructor of Derived, or not?

    No (assuming you’re deleting a Derived object).

    3) Will the fooes vector be deleted automatically while deleting Base or I must delete all members of the class manually?

    Yes & no. The vector itself will be deleted because it is managed automatically, its members will not:

    ~Base ( )
    {
       for ( size_t i = 0 ; i < fooes.size() ; i++ )
         delete fooes[i];
    };
    

    You should have a delete and delete[] for every new and new[] respectively.

    A better alternative to all this is using smart pointers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi mate i have a thread with this code private final class Consumer extends
The following code from my team mate works: public T Get<T, V>(V repo, string
I have this tag: <a id=link1 runat=server href=http://www.selab.isti.cnr.it/ws-mate/example.pdf title=PDF>Open iFrame</a> This link opens a
package com.services { import com.asfusion.mate.events.ResponseEvent; import com.events.navigation.DesgManagementEvent import flash.events.EventDispatcher; import mx.controls.Alert; public class UserManager
I have rjecnik.txt file that looks like this mate sime, jure stipica gujo, prvi
Here's the sample code: user$ touch file.txt user$ mate !$ mate is calling textmate
I am currently writing naive network code for a project and a mate hinted
Basically my mate has been saying that I could make my code shorter by
So I recently discovered this Source Mate tool for Flash builder with all the
I found this example in jboss's documentation. select new Family(mother, mate, offspr) from DomesticCat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.