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 7521073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:10:26+00:00 2026-05-30T02:10:26+00:00

When the object a is destroyed, is the personsInHouse map also destroyed or I

  • 0

When the object a is destroyed, is the personsInHouse map also destroyed or I need to destroy it in destructor? Will it create memory leak if I don’t?

class A {
public:
    map<unsigned int, unsigned int> personsInHouse;
};

int main(){
    A a;
   A.hash[10] = 23;
};
  • 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-05-30T02:10:27+00:00Added an answer on May 30, 2026 at 2:10 am

    Yes, it is. When the destructor of a class is run, the destructors of all its members are run. To be precise, the order is:

    1. Body of the destructor is run
    2. All members, in reverse order of construction, are destructed
    3. All non-virtual base classes, in reverse order of construction, are destructed
    4. All virtual base classes, in reverse order of construction, are destructed

    In general, if you don’t have pointers, you can expect to also not have memory leaks. This is not always the case: you may be using a leaky function, or some function may be performing dynamic allocation and then returning a reference to the object. The situation can be further improved by using smart pointers.

    A useful technique for avoiding memory leaks in C++ is RAII: all standard containers follow it, which is why there’s no need to explicitly clear() a container before it goes out of scope. The basic principle is to make classes clean up all their resources in their destructors, and then make dedicated classes for that so that most of your classes need not worry about it.

    Do note that “class members” are strictly non-static members defined at class scope. If you have

    struct S {
        int* p;
    };
    

    then p is the only member of S, and when S goes out of scope, p will be destroyed (which doesn’t generally involve anything happening, except perhaps an adjustment of the stack pointer). If you at some point do S s; s.p = new int; then p will still be the only member, and the object pointed to by p will not be one, and will therefore not be destroyed when s goes out of scope. For that to happen, you will need to manually do delete s.p;, which corresponds to the general rule of every new needing to have a corresponding delete (idem for new[] and delete[]).

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

Sidebar

Related Questions

When i call object.Dispose() ; Will CLR immediately destroy the object from memory or
Will signals automatically disconnect, when target object is destroyed? Without recording the signal id
Can I trust that an object is destroyed and its destructor is called immediately
I would like to create in C++ a Notifier class that I will use
How does container object like vector in stl get destroyed even though they are
I know that when a constructor fails, the completed member objects will be destroyed.
Object o = new Long[0] System.out.println( o.getClass().isArray() ) System.out.println( o.getClass().getName() ) Class ofArray =
For debugging reasons I want to destroy a class instance which still as references.
See this code here: class test { int n; int *j; public: test(int m)
I have a simple class: public class Runner { public void RunAndForget(RunDelegate method) {

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.