Download MetaTrader 5

File Issue

To add comments, please log in or register
Ernie Gunning
1147
Ernie Gunning  

Hi All,

 When I attach my Expert first time and remove it via the terminal (right click on smiley and remove expert) it removes with no error. If I re-add me expert and remove it again the second time the terminal takes very long and give a Abnormal program termination error after while. I initially found some DeInit methods not being called to free my objects, but after that still, I managed to debug to the point where it is something to do with my file handler... how I know this? If i bypass this Economic calendar class (code) it removes my expert fine on second third...100th time.

I do FileClose after every use of my file handler and I do the FileFindClose on te DeInit method of my class, What am I doing wrong here?

Here is my class, My entry call is the original OnTick() :)

 A short high level explanation, I enter OnTick() and if the calendar is not already loaded (Time stamp of file differs..) I load it (csv file) . If the Calendar is already loaded (timestamp does NOT differ) then I continue.

//+------------------------------------------------------------------+
//|                                                ForexCalendar.mqh |
//|                                                    Ernie Gunning |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Ernie Gunning"
#property link      ""
#property strict
#include <OwnObjects\OOLib\Signals\CalendarEvent.mqh>

class CCalendar
  {
private:

   long EventID[];
   datetime EventTime[], DateLoaded,CreationDate[];
   string EventDetails[,6];
   int FileHandle;
   void CreateEventObj(int index, CCalendarEvent &event);
   void CCalendar::CleanNULLObject(long &EventIdList[]);
public:
                     CCalendar(void)
                     {
                        if(!IsCalendarLoaded())
                        {
                           LoadCalendar();
                        }
                     };
                    ~CCalendar(void){};
                    void LoadCalendar();
                    bool IsCalendarLoaded();
                    //void GetNextCalendarEvent(CArrayObj &events, string NewsOfCountrySymbol=NULL);
                    bool GetNextCalendarEvent(CCalendarEvent &event,string NewsOfCountrySymbol=NULL);
                    bool OnTick();
                    void DeInit(){
                                         //FileClose(FileHandle);
                     FileFindClose(FileHandle);
                    };
   
  };
  
  bool CCalendar::IsCalendarLoaded(void)
  {
      bool result = false;
      FileHandle = FileOpen("Forex.csv", FILE_SHARE_READ,',');
      if(FileHandle!=INVALID_HANDLE)
      {
         datetime fileDate = (datetime)FileGetInteger(FileHandle,FILE_MODIFY_DATE);
         if(fileDate<=DateLoaded)
            result = true;
      }
      return result;
  }
  
  bool CCalendar::OnTick(void)
  {
      if(!IsCalendarLoaded())
         LoadCalendar();
      return true;
  }
  
  void CCalendar::LoadCalendar(void)
  {
      if(!IsCalendarLoaded())
      {
         FileHandle = FileOpen("Forex.csv", FILE_SHARE_READ,',');
         if(FileHandle!=INVALID_HANDLE)
         {
            //Get Past headers on the first line
            for(int i=0;((!FileIsLineEnding(FileHandle))&&(i<100));i++)
            {
               string line = FileReadString(FileHandle);
               bool f = !FileIsLineEnding(FileHandle);
            }
         
            for(int i=0;(!FileIsEnding(FileHandle));i++)
            {
               int size = ArraySize(EventID);
               if(i>=size)
               {
                  ArrayResize(EventID,(size+20),20);
                  ArrayResize(CreationDate,(size+20),20);
                  ArrayResize(EventTime,(size+20),20);
                  ArrayResize(EventDetails,(size+20),20);
               }
               //Read EventId
               string tempString = FileReadString(FileHandle);
               EventID[i] = StringToInteger(tempString);
               //Read CreationDate
               tempString = FileReadString(FileHandle);
               CreationDate[i] = StringToTime(tempString);
               //Read EventTime
               tempString = FileReadString(FileHandle);
               EventTime[i] = StringToTime(tempString);
               //Read Country
               EventDetails[i,0] = FileReadString(FileHandle);
               //Read Impact
               EventDetails[i,1] = FileReadString(FileHandle);
               //Read Actual
               EventDetails[i,2] = FileReadString(FileHandle);
               //Read Previous
               EventDetails[i,3] = FileReadString(FileHandle);
               //Read Forecast
               EventDetails[i,4] = FileReadString(FileHandle);
               //Read Description
               EventDetails[i,5] = FileReadString(FileHandle);
               
               //If we have extra columns after this then lets get past them
               for(int x=0;((!FileIsLineEnding(FileHandle))&&(x<100));x++)
               {
                  string line = FileReadString(FileHandle);
               }
            }
            //Clean empty Arrays.
            CleanNULLObject(EventID);
            DateLoaded = (datetime)FileGetInteger(FileHandle,FILE_MODIFY_DATE);
            FileClose(FileHandle);
         }
         else
         {
            Alert("Unable to load Economic CCalendar as the file (Forex.csv) does not exist.");
         }
      }
  }
  
  
Ex Ovo Omnia
3348
Ex Ovo Omnia  
ToolMaker:

...

I do FileClose after every use of my file handler

...

Actually you do not, in IsCalendarLoaded()
To add comments, please log in or register