MySQL library for MQL4 with proper memory management – library MetaTrader 4

Original library file mql4-mysql.mqh is located here: . Give respect to the original authors for the work done (originally created by Russell, then modified by vedroid and lukins / Sergey) and read description of the original code.

The original mql4-mysql.mqh library had a serious problem with memory management. Problems were reported by several users (you can read about it here:

Since I wanted to use library as part of my projects, I simply couldn’t afford memory build up which would result in crash of the system. After examination of the code I managed to trace the problem to the FetchArray function, which allows user to read and save data from database. Memory wasn’t properly released or allocated, so I made several changes to the code that should prevent memory leak.

Test of the improved library was made and the memory consumption remained stable. Test lasted for several hours and included several thousand connections/read of data with minimal increase of memory usage (less then 10 MB), which is probably the result of MetaTrader 4 itself and indicators running (not a clean install of MetaTrader 4 terminal). From my previous test, the original library couldn’t handle more then a few reads from database and only worked for several minutes before collapsing. Test was done with EA, which requested data on every tick.

IMPORTANT: Read original description of library for example!

int     dbConnectId = 0;

Should be declared as local variable, not as a global. If you use library with EA or indicator, then local declaration is necessary. This is done so that with every iteration, the variable memory address is different (automatically allocated by MetaTrader 4) and released after the cycle.

Alternative:  WPR_DiverSign - indicator MetaTrader 5

You should also open connections for every specific task. Maintaining connection open for several different requests isn’t advised. Example: I want to read several rows from database and save it into array, I declare this procedure in a function. When function is called, connection is established and when data is read, connection is closed and after that function returns XX data. Connection is opened for minimum amount of time.

Always remember to close connection with deinit_MySQL, if you don’t, connections will build up and with time you will reach MAX number of connections allowed by server running your database. Server will crash.

Also proper transaction management should be implemented, so that two or more connections don’t interfere with each other. Can cause read data violation, this will stop your EA from working.

Apache server with MySQL DB for those who needed it (free software):

If you want simple DB, server and DB management tool (it’s free and friendly for users):

Read this for how to set protection for DB:

Test it, please give feedback. That’s the only way problems can be fixed.

Info:

I could write a forum post with detailed examples on how to use the library and what is needed to use it.

This is quite a substantial subject, so I will only write it if there’s need for it. Post a request if you want me to write a user manual.

📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks