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

  • Home
  • SEARCH
  • 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 7858483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:27:38+00:00 2026-06-02T21:27:38+00:00

A simple question here, I have several classes in my code, but only one

  • 0

A simple question here, I have several classes in my code, but only one of them exhibits this issue and I cannot for the life of me work out why. When I create an instance of the class, the destructor is called straight after, yet the instance of the class does not actually appear to be deleted.

Maybe I could live with that if there were not delete[] operations in the destructor that DO affect the instance of the class.

I read somewhere about the ‘rule of three’ or something, so attempted to see what I was missing. I already have a default constructor as well as a user-defined one. I then added what I think is called a copy-constructor, something like this:

MyClass::MyClass(const MyClass &duplicate)
{
    variable1 = duplicate.variable1;
    variable2 = duplicate.variable2;
    // etc
}

What am I missing here that could cause this issue?

EDIT: The requested code. I’ve renamed everything so that it’s all clear (this code still compiles with the issue). First, the header file, MyClass.h:

#ifndef MYCLASS_H
#define MYCLASS_H
#ifndef UNICODE
#define UNICODE
#endif
#include <string>

class MyClass
{
public:
    MyClass();
    MyClass::MyClass(const MyClass &);
    MyClass(int, std::wstring inputWord, int);
    ~MyClass();

    int intOne;
    int intTwo;
};
#endif

Next MyClass.cpp:

#include "MyClass.h"
#include <Windows.h>


MyClass::MyClass(const MyClass &duplicate)
{
    intOne = duplicate.intOne;
    intTwo = duplicate.intTwo;
}
MyClass::MyClass()
{
}

MyClass::~MyClass()
{
    MessageBox(NULL, TEXT("TEST"), TEXT("TEST"),0);
}

MyClass::MyClass(int intOneInput, std::wstring stringInput, int intTwoInput)
{
    intOne = intOneInput;
    intTwo = intTwoInput;
}

And finally how I’m creating my object:

MyClass test(0, TEXT("TEST"), 0);

[Copied from op’s comment]

Actually, scratch my last comment, the deconstructor is NOT called with that particular line (until it goes out of scope), the line that does is words.push_back(MyClass(0, TEXT("TEST"), 0)); declared as std::vector<MyClass> words

  • 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-02T21:27:39+00:00Added an answer on June 2, 2026 at 9:27 pm

    The destructor should be called when the object is destroyed. If you create the object with new, The destructor will be called when you call delete on the object. Otherwise it should be called when it goes out of its scope. You can set breakpoint inside its destructor and see the callstack to check what is calling the destructor. Hope it helps.

    [Update]
    Try to add the below printf for all ctor and dtor to make it sure you are not confused with temporarily created objects.

    printf("ctor %p\n", this); // in constructors
    
    printf("dtor %p\n", this); // in destructor
    

    [Update]

    words.push_back(MyClass(0, TEXT("TEST"), 0));
    

    This creates a temporary object as stl containers(like vector) always “copy” things to store. (Unless “move” happens. I don’t want to start explaining “move” and rvalue here.)

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

Sidebar

Related Questions

this is probably too simple of a question, but here I go. I have
Variants of this question have been asked several times now here, but my question
I have -- what I think -- is a simple question. Here's my code:
I have one question but it can probably be answered by one of several
This is probably a simple question, but it's driving me crazy! I have a
pretty simple question I have here, but I couldn't find the answer: Assume I
I have a simple unit test case (extensive question here ) on a configuration
A little stuck here. I have a simple question I guess. Given the following
I'm trying to learn WPF, so here's a simple question, I hope: I have
I have a simple stupid question. Here's the problem, for years, my company has

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.