Return my custom class from a function (copy constructor not found). - page 2

 
whroeder1:
Using pointers just gets around the OP's post. If you want a copy constructor, make one.The "this." is optional.
Sure... If you want to ignore memory management and encourage poor form...
 
whroeder1:
Using pointers just gets around the OP's post. If you want a copy constructor, make one.The "this." is optional.

Thanks for posting this, that's actually what I was looking for initially in my OP. I've seen how to do that but couldn't find it again! Regarding the example I posted, it is probably more appropriate for me to return a pointer from that function when I don't need that candle twice? My program just needs to look at that candle and then it could be disposed. There are actually 7 MBs of leaked objects when I remove my indicator as of now.

 
nicholishen:
In order to preserve the site's syntax highlighting you have to do all your code editing in the SRC popup window before you hit insert. 

You're asking for a memory leak anytime you instantiate a dynamic object from a temporary scope (ie. function) and start passing it elsewhere without any memory management. If you aren't properly managing that memory then you're going to get a leak, and on the surface you had zero memory management implemented. The object collection classes in the std lib have memory management built in so here are some examples of how you can safely create dynamic objects and pass them out. (Use debugger to step into so you know what's going on behind the scenes)


Thanks again so much for writing this down! I've imported your code and will study it and adapt it to my needs. I'd like to incorporate proper memory management for the objects. I'll get back to your post with some questions for sure. Trying on my own first!

I've been learning to code with a bit of Objective-C and then Apple's Swift since it released. I know my way around that quite well, but memory management is almost completely handled by itself there.

 
ZetaTrader:

Thanks again so much for writing this down! I've imported your code and will study it and adapt it to my needs. I'd like to incorporate proper memory management for the objects. I'll get back to your post with some questions for sure. Trying on my own first!

I've been learning to code with a bit of Objective-C and then Apple's Swift since it released. I know my way around that quite well, but memory management is almost completely handled by itself there.


Yeah... you have to remember that MQL does not have garbage collection like other higher level languages, and like c++ you have to explicitly delete every single object you dynamically allocate using the keyword new


Here are some general guidelines to follow for memory management.  

class Foo
{
protected:
   CObject *m_object;
public:
   Foo()
   {
      m_object = new CObject;
   }
   ~Foo()
   {
      if(CheckPointer(m_object) == POINTER_DYNAMIC)
         delete m_object;
   }
};


void OnStart()
{
   Foo *foo = new Foo;
   delete foo;
   
   CArrayObj statically_allocated_collection;
   foo = new Foo;
   statically_allocated_collection.Add(foo);
   //Automatically deletes all dynamic objects in collection when scope runs out on static collection
}
 
nicholishen:

Yeah... you have to remember that MQL does not have garbage collection like other higher level languages, and like c++ you have to explicitly delete every single object you dynamically allocate using the keyword new


Here are some general guidelines to follow for memory management.  


Hi nicholishen, thanks to the examples you provided I was able to cut down the leakage by a huge amount to 1 Kilobyte!

There are a few objects and strings left that I can't seem to trace but I'll read up further on this.

I guess 1 Kilobyte is a non-issue but this kinda made me ambitious. 

I'll probably have a few questions regarding memory management in the future but I'll move that to a new thread to keep things tidy here since this OP has been answered.

 
ZetaTrader:

Hi nicholishen, thanks to the examples you provided I was able to cut down the leakage by a huge amount to 1 Kilobyte!

There are a few objects and strings left that I can't seem to trace but I'll read up further on this.

I guess 1 Kilobyte is a non-issue but this kinda made me ambitious. 

I'll probably have a few questions regarding memory management in the future but I'll move that to a new thread to keep things tidy here since this OP has been answered.


Any amount of leakage is a huge issue. There is no garbage collection in MQL so you are responsible for explicitly deleting every dynamic object that is allocated by your program in one way or another.