Download MetaTrader 5

Debugging undelete object

To add comments, please log in or register
Have you discovered something really important? Write an article and receive 200 USD!
williamwong
632
williamwong 2010.10.09 06:14 

Does anyone have any idea how to trace the program to find out which objects are not deleted?  I got a few undeleted objects in my EA after running Strategy tester.  Thanks.

hasayama
610
hasayama 2010.10.09 13:02  
williamwong:

Does anyone have any idea how to trace the program to find out which objects are not deleted?  I got a few undeleted objects in my EA after running Strategy tester.  Thanks.

Give every object that you use a specific prefix in the name, for ex. "!&&_", define the following function:

void obj_delete ( string prefix )
{       
        string  name    = "";
        int             total   = ObjectsTotal(0) - 1;
        
        for ( int i = total; i >= 0; i -- )
        {
                name = ObjectName( 0, i );
                if ( StringFind( name, prefix ) >= 0 ) { ObjectDelete( 0, name ); }
        }
}

 In stead of 0 you can use any window index.

 In OnDeinit() call obj_delete( "!&&_" ); - it should clean all the objects that you've created by EA.

williamwong
632
williamwong 2010.10.13 17:14  
hasayama:

Give every object that you use a specific prefix in the name, for ex. "!&&_", define the following function:

 In stead of 0 you can use any window index.

 In OnDeinit() call obj_delete( "!&&_" ); - it should clean all the objects that you've created by EA.

I am sorry, I was refering to object created via new operator.  How do I track what objects are not deleted after the program ends?
Alexey Da
Moderator
6606
Alexey Da 2010.10.13 17:50  
williamwong:
I am sorry, I was refering to object created via new operator.  How do I track what objects are not deleted after the program ends?
Could you attach source?
MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
Alexander Piechotta
6954
Alexander Piechotta 2010.10.13 18:40  
How do I track what objects are not deleted after the program ends?
void obj_Print ( string prefix )
{       
        string  name    = "";
        int             total   = ObjectsTotal(0) - 1;
        
        for ( int i = total; i >= 0; i -- )
        {
                name = ObjectName( 0, i );
                if ( StringFind( name, prefix ) >= 0 ) { Print( 0, name ); }
        }
}

williamwong
632
williamwong 2010.10.14 11:41  
alexvd:
Could you attach source?

In some programs, I wrote my own garbage collector using CList to keep all the dynamic objects created and clear them in destructor via CList destructor during program exit.  This is because I was careful to take care of unused pointers.  But in other programs, I may overlook and the program exits with undeleted objects.  The log file just mentioned the number of undeleted objects without saying what objects they are.  And to trace them through stacks of function calls will be difficult. 

Is there a system pool that I can peek to see what objects are left over in debugger?

williamwong
632
williamwong 2010.10.14 11:42  
FinGeR:

I was not refering to graphical objects.  I was refering to objects as in OO sense created by new operator and should be destroyed by delete operator.
Alexey Da
Moderator
6606
Alexey Da 2010.10.14 11:52  
williamwong:

In some programs, I wrote my own garbage collector using CList to keep all the dynamic objects created and clear them in destructor via CList destructor during program exit.  This is because I was careful to take care of unused pointers.  But in other programs, I may overlook and the program exits with undeleted objects.  The log file just mentioned the number of undeleted objects without saying what objects they are.  And to trace them through stacks of function calls will be difficult. 

Is there a system pool that I can peek to see what objects are left over in debugger?

At this moment it is impossible, but we will discuss it.

Rashid Umarov
Admin
11826
Rashid Umarov 2010.10.14 12:04  
williamwong:
I was not refering to graphical objects.  I was refering to objects as in OO sense created by new operator and should be destroyed by delete operator.
Have you seen article The Order of Object Creation and Destruction in MQL5?
williamwong
632
williamwong 2010.10.18 09:57  

Yes.

I would like to ask for a variable to display number of undeleted  objects.

For example, in debug mode, I can display _UNDELETEDOBJECTS, it will show the number of undeleted objects.  In this way, I could at least find out which part of the codes are not deleting the dynamic objects.

Ilyas
1205
Ilyas 2010.10.18 12:41  

use create/destroy function to count objects, something like this:

class CFoo;

int ExtFooCount=0;    // global variable

CFoo *CreateFoo() { ExtFooCount++; return(new CFoo);  }
void DestroyFoo(CFoo *foo) { if(ExtFooCount>0) ExtFooCount--; delete foo; }

class CCntPrint
  {

public:
   ~CCntPrint()
     {
      if(ExtFooCount) Print(ExtFooCount," leaked of CFoo");
      ...
     }
  };
CCntPrint ExtCountsPrinter;   // global variable




12
To add comments, please log in or register