Debugging undelete object

 

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.

 
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.

 
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?
 
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.
 
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 ); }
        }
}

 
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?

 
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.
 
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.

 
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?
 

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.

 

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



Reason: