appleBirdPie
appleBirdPie

Reputation: 45

C++ Why is the destructor immediately called after the object has been constructed in the stack?

I have two unit tests. In the first one I create the object myMovie in the stack. The object is created and immediately afterwards the destructor is called. This causes the unit test to fail because when myMovie falls out of scope, the destructor is called again. This results in an access violation.

However, if I create the object in the Heap, everything works just fine. Why is the destructor immediately called after the object has been constructed in the stack?

First one like this:

TEST_METHOD(constructingMovieWithParametersStack)
    {
        _CrtMemState s1, s2, s3;
        _CrtMemCheckpoint(&s1);
        {
            Movie myMovie = Movie("firstName", "lastName", "title");
            // Why is the destructor is called here???

            string expectedDirectorFirst = "firstName";
            string expectedDirectorLast = "lastName";
            string expectedTitle = "title";
            wchar_t* message = L"movie title wasn't set correctly";
            Assert::AreEqual(expectedTitle, myMovie.getTitle(), message, LINE_INFO());
        }
        _CrtMemCheckpoint(&s2);
        wchar_t* leakMessage = L"there is a leak";
        bool isThereALeak = _CrtMemDifference(&s3, &s1, &s2);
        Assert::IsFalse(isThereALeak, leakMessage, LINE_INFO());
    }

Second unit test like this:

TEST_METHOD(constructingMovieWithParametersHeap)
    {
        _CrtMemState s1, s2, s3;
        _CrtMemCheckpoint(&s1);
        {
            Movie* myMovie = new Movie("firstName", "lastName", "title");

            string expectedDirectorFirst = "firstName";
            string expectedDirectorLast = "lastName";
            string expectedTitle = "title";
            wchar_t* message = L"movie title wasn't set correctly";
            Assert::AreEqual(expectedTitle, myMovie->getTitle(), message, LINE_INFO());
            delete myMovie;
        }
        _CrtMemCheckpoint(&s2);
        wchar_t* leakMessage = L"there is a leak";
        bool isThereALeak = _CrtMemDifference(&s3, &s1, &s2);
        Assert::IsFalse(isThereALeak, leakMessage, LINE_INFO());
    }

Here is the Movie class:

#include "Movie.h"

using namespace std;

Movie::Movie()
{
    this->director = new Person();
    this->title = "";
    this->mediaType = 'D';    // for DVD
}

Movie::Movie(string firstName, string lastName, string title)
{
    this->director = new Person();
    this->director->setFirstName(firstName);
    this->director->setLastName(lastName);
    this->title = title;
    this->mediaType = 'D';    // for DVD
}

Movie::~Movie()
{
    delete director;
}

string Movie::getTitle()
{
    return title;
}

Upvotes: 2

Views: 165

Answers (1)

AlexD
AlexD

Reputation: 32576

Movie myMovie = Movie("firstName", "lastName", "title");
// Why is the destructor is called here???

here a temporary object is created and used to copy initialize myMovie, then the temporary is destructed.

Did you mean

Movie myMovie("firstName", "lastName", "title");

Upvotes: 6

Related Questions