0

I am using this code for getting information from a website that I scrape for news and getting these two error messages on the expert log

  1. 806912 bytes of leaked memory
  2. 1 leaked strings left

When I ran get the last error message, I get ERR_INVALID_ARRAY

here is the function that I suspect is causing the error, I am suspecting that it has something to do with these two arrays post[], result[] though I am not sure. Any form of help will be appreciated

string ReadCBOE()
   {
      string cookie=NULL,headers;
      char post[], result[];     
      string TXT="";
      int res;
      string str;
      
      static string str2 = "";
   
      string google_url="http://ec.forexprostools.com/?columns=exc_currency,exc_importance&importance=1,2,3&calType=week&timeZone=15&lang=1";
      ResetLastError();
      int timeout=5000; 
      res=WebRequest("GET",google_url,cookie,NULL,timeout,post,0,result,headers);
      if(res==-1)
      {
         Print("WebRequest error, err.code  =",GetLastError());
         MessageBox("You must add the address 'http://ec.forexprostools.com/' in the list of allowed URL tab 'Advisors' "," Error ",MB_ICONINFORMATION);
      }
      else
      {
         int filehandle=FileOpen("news-log.html",FILE_WRITE|FILE_BIN|FILE_ANSI);
         if(filehandle!=INVALID_HANDLE)
         {
            FileWriteArray(filehandle,result,0,ArraySize(result));
            FileClose(filehandle);
            //------+
            int file_handle=FileOpen("news-log.html",FILE_READ|FILE_BIN|FILE_ANSI);
            if(file_handle!=INVALID_HANDLE)
            {
               do
               {
                  ResetLastError();
                  string Largestr=FileReadString(file_handle,4000);
                  if(GetLastError()!=0) break;
                  StringConcatenate(str,str,Largestr);
               }
               while(GetLastError()==0 && !FileIsEnding(file_handle));
               FileClose(file_handle);
            }
            else PrintFormat("Failed to open %s file, Error code = %d","news-log.html",GetLastError());
         }
      }
   return(str);
   }
Harrison
  • 25
  • 5

2 Answers2

0

I know it's late to help the OP, but...

StringConcatenate was repeatedly reported with this kind of leak-memory problems, especially in loops. Also, it's slower than StringAdd, which I'd use in situations like this.

That said, I didn't understand the reason to write to a log file and immediately read from it when the data is already in a variable. It could be much easier/faster using CharArrayToString right before return it.

Wilfredo Pomier
  • 1,091
  • 9
  • 12
0

I would suspect that the reason why you are seeing leaked memory is because you are not handling your error(s) properly. From what I understand, there is no garbage collector in c++, which I believe MQL is a subset of See here why.

As such, when an exception is being thrown, you still have allocated memory that is not released (i.e. your string). Try clearing the string (set to null) when an error is thrown. Think that may remove the error leak issue.

Dean
  • 2,326
  • 3
  • 13
  • 32