About Store Forum Documentation Contact
Donations:
215$/mo



Post Reply 
Memory Container on Extended Pointer Leak
Author Message
SamNainocard Offline
Member

Post: #1
Memory Container on Extended Pointer Leak
Hi, I'm have a problem with memory leak when using Memory Container inside extended class, that have another class have a pointer that pointed to Base class, and when every create a new element it will leak, however it will leak only once.

Here's example
Code:
/******************************************************************************/
class TEST : BASE_CLASS
{
   Memx<int> mem2;
   virtual void add(){mem2.New()=0;}
}
class BASE_CLASS
{
   //Memx<int> mem1;
   virtual void add(){/*mem1.New()=0;*/}
}
class POINTER
{
   BASE_CLASS * t=null;
   ~POINTER() {delete t;}
}
POINTER test1,
        test2;
/******************************************************************************/
void InitPre()
{
   App.flag=APP_MEM_LEAKS;
   EE_INIT();
}
/******************************************************************************/
bool Init()
{
   test1.t=new BASE_CLASS();
   test2.t=new TEST();
   test1.t.add();  // Not Leak
   test1.t.add();
   test2.t.add();  // Leak
   test2.t.add();
   return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
bool Update()
{
   if(Kb.bp(KB_ESC))return false;
   return true;
}
/******************************************************************************/
void Draw() // main drawing
{
   D.clear(TURQ);
   D.text (0, 0, "Hello to Esenthel Engine !");
}
/******************************************************************************/
Once create new element, test2 will leak for 3 (2 for Memb, 1 for Memc) even with additional element or clear before delete.

How do I fix this?

Thank you in advance. smile
(This post was last modified: 08-09-2015 03:16 PM by SamNainocard.)
08-09-2015 02:29 PM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #2
RE: Memory Container on Extended Pointer Leak
try adding virtual ~BASE_CLASS
08-09-2015 02:33 PM
Find all posts by this user Quote this message in a reply
SamNainocard Offline
Member

Post: #3
RE: Memory Container on Extended Pointer Leak
Sorry, but what should I put into ~BASE_CLASS()?

By the way, I forget that 'mem1' doesn't leak, so I'll comment 'mem1' from code.
08-09-2015 03:16 PM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #4
RE: Memory Container on Extended Pointer Leak
nwm, forum wasn't displaying properly.

BASE_CLASS * t=null;
~POINTER() {delete t;}

in this case you really should be doing
Code:
if(t){
delete t;
t=nullptr;
}

you shouldn't need to add anything to the virtual ~BASE, this is just to make sure that the higher object makes sure to call destruction every level.

without the virtual destructor it will not destruct the class that extends it, hence both class destructors needs to be declared.

Code:
struct TestA{
    virtual ~TestA(){}
};

struct TestB : TestA
    virtual ~TestB(){}
};

See if that doesn't help

http://stackoverflow.com/questions/46120...estructors
This link explains what is going on.
(This post was last modified: 08-09-2015 06:06 PM by Zervox.)
08-09-2015 03:35 PM
Find all posts by this user Quote this message in a reply
SamNainocard Offline
Member

Post: #5
RE: Memory Container on Extended Pointer Leak
Oh, I forget to add "virtual", now adding "virtual ~BASE_CLASS()" does fix the problem, I never know about virtual deconstructor.

TYVM. smile
08-09-2015 05:19 PM
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #6
RE: Memory Container on Extended Pointer Leak
I would say that one should always use virtual for destructors as you never know if anyone will inherit your class. I really cant see any case where you shouldn't have a virtual destructor, at least if something is done in your destructor.
(This post was last modified: 08-09-2015 07:34 PM by rstralberg.)
08-09-2015 07:33 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #7
RE: Memory Container on Extended Pointer Leak
(08-09-2015 07:33 PM)rstralberg Wrote:  I would say that one should always use virtual for destructors as you never know if anyone will inherit your class. I really cant see any case where you shouldn't have a virtual destructor, at least if something is done in your destructor.

Actually, you shouldn't make everything use virtual destructor or virtual functions for that matter, it's a waste of performance.
You should only use virtual destructors when you deal with code that you only keep in base classes that will delete an object through a non extending pointer like Sam does in this example.
(This post was last modified: 08-10-2015 05:31 AM by Zervox.)
08-10-2015 05:16 AM
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #8
RE: Memory Container on Extended Pointer Leak
(08-10-2015 05:16 AM)Zervox Wrote:  
(08-09-2015 07:33 PM)rstralberg Wrote:  I would say that one should always use virtual for destructors as you never know if anyone will inherit your class. I really cant see any case where you shouldn't have a virtual destructor, at least if something is done in your destructor.

Actually, you shouldn't make everything use virtual destructor or virtual functions for that matter, it's a waste of performance.
You should only use virtual destructors when you deal with code that you only keep in base classes that will delete an object through a non extending pointer like Sam does in this example.
Exactly. Wasn't it that what I said. I was only talking about destructors that do anything. When coming to virtual methods its a completely different thing of course. Here you should use the virtual only when its actually needed.
08-10-2015 07:15 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #9
RE: Memory Container on Extended Pointer Leak
From what I understood you said to use virtual destructors everywhere because there is no reason not to, and I am sorry if I misunderstood you.

Virtual destructors cost performance too and is a waste if your class is not supposed to be extended.

design a class for what it is meant to do not what some other person MIGHT want it to do. if you write every class to follow the reasoning that "someone might do" you will essentially be overengineering everything and maybe loose valuable performance in the process.

It's not about destructors that do something, you need virtual destructors ONLY if a person does
Base *der=new Derived();
if it was
Derived *der=new Derived();
it doesn't need a virtual destructor as it will call Base destructor automatically.
the reason for virtual is only if you go from base to derived.

again sorry if I misunderstood.
08-10-2015 07:44 AM
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #10
RE: Memory Container on Extended Pointer Leak
(08-10-2015 07:44 AM)Zervox Wrote:  From what I understood you said to use virtual destructors everywhere because there is no reason not to, and I am sorry if I misunderstood you.

Virtual destructors cost performance too and is a waste if your class is not supposed to be extended.

design a class for what it is meant to do not what some other person MIGHT want it to do. if you write every class to follow the reasoning that "someone might do" you will essentially be overengineering everything and maybe loose valuable performance in the process.

It's not about destructors that do something, you need virtual destructors ONLY if a person does
Base *der=new Derived();
if it was
Derived *der=new Derived();
it doesn't need a virtual destructor as it will call Base destructor automatically.
the reason for virtual is only if you go from base to derived.

again sorry if I misunderstood.
Not a problem. You are perfectly right smile

(08-10-2015 07:44 AM)Zervox Wrote:  From what I understood you said to use virtual destructors everywhere because there is no reason not to, and I am sorry if I misunderstood you.

Virtual destructors cost performance too and is a waste if your class is not supposed to be extended.

design a class for what it is meant to do not what some other person MIGHT want it to do. if you write every class to follow the reasoning that "someone might do" you will essentially be overengineering everything and maybe loose valuable performance in the process.

It's not about destructors that do something, you need virtual destructors ONLY if a person does
Base *der=new Derived();
if it was
Derived *der=new Derived();
it doesn't need a virtual destructor as it will call Base destructor automatically.
the reason for virtual is only if you go from base to derived.

again sorry if I misunderstood.
Not a problem. You are perfectly right smile
I was quite unclear and didn't think of this last case.
So there you go all. Zervox have the absolute correct solution to this.
(This post was last modified: 08-10-2015 07:49 AM by rstralberg.)
08-10-2015 07:47 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #11
RE: Memory Container on Extended Pointer Leak
(08-10-2015 07:44 AM)Zervox Wrote:  ...
design a class for what it is meant to do not what some other person MIGHT want it to do. if you write every class to follow the reasoning that "someone might do" you will essentially be overengineering everything and maybe loose valuable performance in the process.
...

This is good advice ... I like this smile

Thanks also guys for the discussion on the use of virtual destructors, a handy reminder.
08-10-2015 04:12 PM
Find all posts by this user Quote this message in a reply
Post Reply