Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Memory Delete?  (Read 3794 times)

0 Members and 1 Guest are viewing this topic.

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Memory Delete?
« on: October 28, 2012, 01:33:39 am »
Hey all.

Been working on my game recently using a state manager that Exploiter developed.

I have made a physics manager class in which I am going to add all my Box2D stuff.

Header
#pragma once
#include <Box2D/Box2D.h>

class PhysicsManager
{
public:
        PhysicsManager(void);
        ~PhysicsManager(void);

private:
        b2World *mWorld;
};

 

cpp
#include "PhysicsManager.h"
#include <iostream>

PhysicsManager::PhysicsManager(void)
{
        b2Vec2 Gravity(0.f, 9.4f);
    mWorld = new b2World(Gravity, false);

}

PhysicsManager::~PhysicsManager(void)
{
        std::cout << "Deleting Physics Manager" << std::endl;
        delete mWorld;
}

 

as you can see, very simple - all it does is make a new mWorld at the moment and delete it in its destructor.

Now, say I allocate this in my 'Play' state for instance -

class PlayState : public GameState
{
public:
        PlayState( GameEngine& game, bool replace = true );

        void pause();
        void resume();

        void update();
        void draw();

private:
        PhysicsManager mPhysicsManager;
};

 

now, this will allocate the mPhysicsManager on the stack, so when PlayState is killed when the app ends, mPhysicsManager will go out off scope so its destructor should be called correct? Thus deleting mWorld in my Physics Manager class.

but this doesn't happen. I get a horrible memory leak coming from mWorld..

if I explicitly call the destructor in my playstate constructor (so it deletes it straight after its created) it cleans up fine.. just not when playstate goes out of scope.
« Last Edit: October 28, 2012, 01:39:41 am by 1337matty »
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Memory Delete?
« Reply #1 on: October 28, 2012, 01:54:57 am »
This is not sfml related but it's box2d so I'm interested
How did you 'detect' the horrible leak of doom?
May I also ask why don't you use latest box2d?
Back to C++ gamedev with SFML in May 2023

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: Memory Delete?
« Reply #2 on: October 28, 2012, 02:06:41 am »
I use VLD Memory Detector, and it's going ape shit over mWorld when I close my program - basically its not being deleted - the destructor is not being called for some reason I think..

I just haven't updated yet, CMake demotivates me to update it :P
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Memory Delete?
« Reply #3 on: October 28, 2012, 02:19:43 am »
Since the play state is derived from GameState the destructor needs to be defined virtual, which I think is not the case atm, otherwise the child's class destructor won't be called.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: AW: Memory Delete?
« Reply #4 on: October 28, 2012, 02:22:02 am »
Since the play state is derived from GameState the destructor needs to be defined virtual, which I think is not the case atm, otherwise the child's class destructor won't be called.

Hey buddy.

Could you explain that a little bit better so I fully understand whats going on?

Which destructor are you talking about?
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Memory Delete?
« Reply #5 on: October 28, 2012, 02:23:51 am »
I think box2d switched to premake instead of cmake.
Did your text in d-tor output to console? Try adding another pair of sharp brackets to make scope of your manager smaller than main's scope, maybe vld runs at the end of main but before d-tors. I'm not sure, never dealt with it.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Re: AW: Memory Delete?
« Reply #6 on: October 28, 2012, 02:28:03 am »
Could you explain that a little bit better so I fully understand whats going on?

Which destructor are you talking about?
You need to add virtual ~GameState(); to the GameState header and then implement a destructor for each state.

Btw a nicer solution would be to use std::unique_ptr.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: Memory Delete?
« Reply #7 on: October 28, 2012, 02:43:32 am »
hmm, okay. But why do I need to do this?

I thought the way the states work is as they are just allocated on the stack, when they go out of scope they are automatically deleted, now when that happens, technically doesn't that make 'PhysicsManager' go out of scope? Thus calling his De-tor which cleans deletes the mWorld memory?
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Memory Delete?
« Reply #8 on: October 28, 2012, 02:45:41 am »
Idk exploiters engine, but if something gets deleted via pointer to base then base must have virtual d-tor.
Back to C++ gamedev with SFML in May 2023

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: Memory Delete?
« Reply #9 on: October 28, 2012, 02:21:25 am »
Yeah that's fixed the problem.. Man been doing C++ at University for 2 years and I still miss shit like this, i'm pathetic lol :P

Thanks again boyos :)
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Re: Memory Delete?
« Reply #10 on: October 28, 2012, 07:35:11 am »
hmm, okay. But why do I need to do this?
You can get a bit more details here. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything