Why is my destuctor not running? or is it?

Write your question here.
main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "Player.h"


int main ( int argc, const char * argv[] )
{

    {
        Player* newPlayer = new Player;
        char *username =
            "sldkjflkjdfljflkdfdfljdfjldkjsf;kjf;aslkjd;ljd;fla;slkdfja;lskjdf;alsjkdf;ajksdf;lajksdf;lakjsdf;klajsd;flakjsd;flkjas;dlfjka;sldkjfa;lsdjkf;alksjdf;alsdjkf;aksljdf;alksdjf;alksdjf;laksdjf;lasdjf;lakdjsf;lkasdjf;kaljsd;fkja;sdkjf;aklsdjf;askldjf;aksjdf;lkajsd;fjkas;dkjf;askldjf;alskdjf;afasd;lfka;slkdfja;lskdfj;alksjdf;lkajsd;flkjas;ldfjka;lskdfj;alskdjfla;ksdjf;alskdjf;alksdjf;alskdjf;laskdjf;alksdjf;laksjdf;lkjsdf;lkjas;flkjas;dlfjka;slkfja;lsdfjk;aslkdjf;alskdjf;alskdjf;alksdjf;laksjdf;lkasjdf;lkajsdf;lkasdjf;lkajsdf;klajsdf;lkajsdf;lkajsdf;laksjdf;klajsdf;aklsdjf;alskdjf;alskdjf;alksjdf;laskdjf;lkadjs;flkjas;dflkja;sldfjk;aslkdfjaksjfhlas;fdjklashfjklasdkfjlasdlkfj;asldkjf;alskdjf;alskdfjalksdjf;alksdjf;laksdj;falksdjf;aklsdjf;alksdhf;kajsdhfgljkasdhgflkajdfhsglsjdkfghlsdkghjlsdkjfghalsdkfj;askdfj;asdljkf;alskdjf;alskjdf;laksjdhflaskdfha;sdlfjkhas;dfkh;askldjf;aslkjdf;alskdjf;asjdfhkasd;fklslskdjf;alksjdf;alksjdf;lakjsd;flajksd;fklajsd;flkajsd;flkajsdfkljas;dklfja;sdkjf;aklsdjf;aklsdjf;aklsdfj;alksdjf;alksdjf;lksdjf;alksdfj;asldkfja;sdkjf;asldkfj;aslkdjf;asldkfj;alskdjf;alsdkjf;alskdjf;aklsdjf;asdkljf;asdkljfa;sdlkjf;asdlkfj;asldjkf;alskdjf;asdlkf;alsdjkf;alsdjkf;lsdjkf;laksjdjflkdjfslkdjflskdjflsjflsjdflsdkjflskj";
        newPlayer->set_userName ( username );
    }
    return 0;

Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef __lab3__Player__
#define __lab3__Player__

#include <stdio.h>

class Player
{

    private:
        typedef enum gender
        {
            UNKNOWN,
            MALE,
            FELMALE
            
        } GenderType;
        
        unsigned int level;
        char *usersName;
        GenderType Gender;
        
    public:
        Player();
        ~Player();
        Player ( const Player& a_Player );
        
        void LevelUp();
        void set_Level ( unsigned int level );
        void set_userName ( char *userName );
        void set_Gender ( GenderType a_gender );
        
        unsigned int get_Level();
        char* Get_userName();
        unsigned int get_Gender();
};

#endif /* defined(__lab3__Player__) */

Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "Player.h"

Player::Player() : level ( 0 ), usersName ( nullptr ), Gender ( UNKNOWN )
{
}


Player::Player ( const Player& a_Player )
{
    Gender = a_Player.Gender;
    usersName = a_Player.usersName;
    level = a_Player.level;
}


Player::~Player()
{
    delete usersName;
}


void Player::LevelUp()
{
    level++;
}
void Player::set_Level ( unsigned int a_level )
{
    level = a_level;
}
void Player::set_userName ( char *a_userName )
{
    usersName = a_userName;
}
void Player::set_Gender ( GenderType a_gender )
{
    Gender = a_gender;
}

unsigned int Player::get_Level()
{
    return level;
}
char* Player::Get_userName()
{
    return usersName;
}
unsigned int Player::get_Gender()
{
    return Gender;
}





Last edited on
Why is my destuctor not running? or is it?

Because the object you create is never destroyed and the memory used to store it is leaked. This is probably a good thing since your destructor tries to delete something your class instance doesn't own.
I am not following you..


 
        char *usersName;


is owned by Player.h right?
I never deleted my object.
silly me. Thank you.
Last edited on
I am not following you.. char *usersName; is owned by Player.h right?


Player.h is a header file. It doesn't make sense to speak of ownership for a file.

The pointer member of the class is "owned" by the class, yes. What it points to is not.
if you delete your object that is dynamically allocated, the destructor will not run.

if you wait the object to go out of scope and execute destructor the memory will be leaked
Lorence30 wrote:
if you delete your object that is dynamically allocated, the destructor will not run.
That is not how delete works.
Lorence30 wrote:
if you wait the object to go out of scope and execute destructor the memory will be leaked
That is not how destructors work.
Just in case anyone comes across this the problem was.. [from my perspective and someone feel free to correct me if I am wrong]

The pointer my destructor was attempting to delete was a pointer that was allocated with memory on the stack.
1
2
 
 char *username  "foo"; // from main.cpp 


Then I passed the string created in main.cpp to my class.
 
        newPlayer->set_userName ( username );


and my class would handle the above operation as below.

1
2
3
4
void Player::set_userName ( char *a_userName )
{
    usersName = a_userName;
}


and this was just creating a pointer to the same data that my original pointer pointed to.

then (not sure if my original post shows this) when (if) my destructor ran on the data member inside of my class, my compiler would complain I am trying to "free" a object that is not allocated or as cire said

Because the object you create is never destroyed and the memory used to store it is leaked. This is probably a good thing since your destructor tries to delete something your class instance doesn't own.


as shown below

1
2
3
4
5
Player::~Player()
{
    delete  ;
}


What I did to fix the above issue(s) is I changed

1
2
3
4
void Player::set_userName ( char *a_userName )
{
    usersName = a_userName;
}


to what is listed below. So that the string that I was passing into my class was actually copied to the memory my pointer inside of my class was pointing to.

1
2
3
4
5
6
7
8
void Player::set_userName ( char *a_userName )
{
    if (this->usersName) {
        delete [] this->usersName;
    }
    this->usersName = new char[strlen(a_userName)+1];
    strcpy(this->usersName, a_userName);
}


then I changed my destructor (added a simple test statement as things were working as expected and I was no longer troubleshooting) where I think my original destructor would of worked also.

1
2
3
4
5
6
Player::~Player()
{
    if (usersName) {
        delete [] usersName;
    }
}



Last edited on
@LB

 this is not how delete works. 


I didn't say it only works that way.
So do you mean if he deleted the new player object in the middle of program, the destructor will still execute at the end?
When you delete an object, the destructor is called right then and there. If you never delete a dynamically allocated object, the destructor is never called. Maybe I misread your post, but it seemed like you were saying something else.
I see thanks
Topic archived. No new replies allowed.