Desctructor being called for Unknown reason

A

APT News Address

Sorry if this is a dumb question.

I'm having a problem with a class I designed. For some reason the
destructor is being called before I am done with it and I can't see a
reason for it. The constructor appears to work fine when it is called
for both objects I create but for some reason the destructor is called
immediately after construction in the second of these objects. Because
of this the destructor crashes the program the second time it is
called when the object goes out of scope. Can anyone help me to see
what I'm doing wrong.

Thanks,

-Adrian


The code is as follows:

MainFile.cpp
============
#include "stdafx.h"
#include "Parameter.h"

int _tmain(int argc, TCHAR* argv[])
{
TCHAR* pp = 0;
CParameter P1("Hello");
CParameter P2(pp);
// Between these two lines the destructor for P2 executes
// for some reason, I can't see a reason for this.
return 0;
}

Parameter.h
===========
#pragma once

class CParameter
{
public:

CParameter(void);
CParameter(const TCHAR Name[]);
CParameter( const CParameter &rhs );
~CParameter(void);

const TCHAR* const Parameter(void);
const bool Explicit(void);

inline CParameter& operator=( const CParameter &rhs );

private:
bool _Explicit;
TCHAR* _Parameter;
};

Parameter.cpp
=============
#include "StdAfx.h"
#include ".\parameter.h"

CParameter::CParameter(void)
{
_Parameter = 0;
_Explicit = false;
}

CParameter::CParameter(const TCHAR Name[])
{
if( Name )
{
_Parameter = new TCHAR[ strlen(Name) + 1 ];
strcpy( _Parameter, Name );
_Explicit = true;
}
else
{
CParameter();
}
}

CParameter::CParameter( const CParameter &rhs )
{
_Parameter = 0;
*this = rhs;
}

CParameter::~CParameter(void)
{
delete [] _Parameter;
}

const TCHAR* const CParameter::parameter(void)
{
if( _Parameter )
return _Parameter;
else
return 0;
}

const bool CParameter::Explicit(void)
{
return _Explicit;
}

inline CParameter& CParameter::blush:perator=( const CParameter &rhs )
{
if( this != &rhs )
{
delete [] _Parameter;
if( rhs._Parameter )
_Parameter = new TCHAR[ strlen(rhs._Parameter) + 1 ];
else
_Parameter = 0;
strcpy( _Parameter, rhs._Parameter );
_Explicit = rhs._Explicit;
}
return *this;
}
 
D

doug mansell

That's perfectedly normal behaviour. P1 and P2 are automatic (stack)
variables... so when they go out of scope, they are destroyed. After
the destructor for P2 is called, then the destructor for P1 will be called.

The alternative to stack variables is heap variables - created and
destroyed explicitly with new and delete.
 
D

doug mansell

Actually, i noticed something else.... :)

I'm having a problem with a class I designed. For some reason the
destructor is being called before I am done with it and I can't see a
reason for it. The constructor appears to work fine when it is called
for both objects I create but for some reason the destructor is called
immediately after construction in the second of these objects. Because
of this the destructor crashes the program the second time it is
called when the object goes out of scope. Can anyone help me to see
what I'm doing wrong.


In this constructor (below), you're creating a temporary CParameter in
the else case... so maybe the call to the destructor for this temporary
instance is confusing you.

I think (in the else case) you really just want:

_Parameter = 0;
_Explicit = false;


CParameter::CParameter(const TCHAR Name[])
{
if( Name )
{
_Parameter = new TCHAR[ strlen(Name) + 1 ];
strcpy( _Parameter, Name );
_Explicit = true;
}
else
{
CParameter();
}
}
 
A

APT News Address

Thanks, that explains why the code was crashing, I was assuming the
object was initializing correctly.

doug mansell said:
Actually, i noticed something else.... :)

I'm having a problem with a class I designed. For some reason the
destructor is being called before I am done with it and I can't see a
reason for it. The constructor appears to work fine when it is called
for both objects I create but for some reason the destructor is called
immediately after construction in the second of these objects. Because
of this the destructor crashes the program the second time it is
called when the object goes out of scope. Can anyone help me to see
what I'm doing wrong.


In this constructor (below), you're creating a temporary CParameter in
the else case... so maybe the call to the destructor for this temporary
instance is confusing you.

I think (in the else case) you really just want:

_Parameter = 0;
_Explicit = false;


CParameter::CParameter(const TCHAR Name[])
{
if( Name )
{
_Parameter = new TCHAR[ strlen(Name) + 1 ];
strcpy( _Parameter, Name );
_Explicit = true;
}
else
{
CParameter();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top