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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:19:53+00:00 2026-06-12T15:19:53+00:00

I am having a problem with a destructor being called for a class at

  • 0

I am having a problem with a destructor being called for a class at the end of a subroutine even though it should be defined outside of the scope of the subroutine.

Here is the smallest piece of code I have that displays my problem:

#include <iostream>
using namespace std;

class Foo {
private:

    double *array;

public:

Foo(int N) {
   array = new double[N];
   for (int i=0; i<N; i++) {
       array[i]=0;
   }
}

~Foo() {
   delete[] array;
}
};

void subroutine(Foo x) {
   cout << "Hello!" << endl;
}

int main() {
   Foo bar(10);
   subroutine(bar);
   subroutine(bar);
}

Now the destructor for the object bar here gets called after the first subroutine finishes even though it’s scope should be the whole of the main() function? This means that when I call the second subroutine the destructor is called again and I get a memory leak.

I have found I can fix this by calling by reference in the subroutine but I am not very satisfied with this fix since I dont understand why it didn’t work in the first place.
Can anyone shed some light on this for me?

Thanks.

  • 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-12T15:19:55+00:00Added an answer on June 12, 2026 at 3:19 pm

    You are passing a Foo by value to the subroutine function. This means it has it’s own copy, which gets destroyed on exiting it’s scope.

    void subroutine(Foo x) {
       // x is a new Foo here. It will get destroyed on exiting scope,
       // resulting in a destructor call
    }
    

    Your main problem here is that you have not implemented a copy constructor, so the dynamically allocated array is not copied (only the pointer that points to it is). So, when you copy Foo objects, you have each copy referring to the same array. And each copy will try to destroy it.

    You should follow the rule of three and implement an assignment operator and a copy constructor that make a “deep copy” of the array, such that each Foo object owns its own array.

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

Sidebar

Related Questions

I'm having a weird problem with a threaded class using Boost::threads. Here is a
The problem I am having is that when my class CLimb runs its destructor,
I am having a problem with delete and destructor (I am sure I am
Having problem with this bit of code qith jQuery. it should pick the values
I am having a problem placing an instance of my reference-counting Pointer <Type> class
I'm having trouble with deleting my template. My template and destructor: template<class S, class
I'm having a little bit of problem with my derived class. Basically I have
edit: I should probably say how I am currently worked around the problem here.
Having problem reading bytearray of custom objects. Any help is appreciated public class CustomObject
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how

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.