Hiya friends,
I have a problem with a CQueue that I want to use to store class objects. In one function, I put in class instances into the queue and in another function I want to take them out again. Locally inside the function in which I put them in I can dequeue without any problems, but globally these entries are gone. Here an example of an EA:
So as expected, we can dequeue and read out the first entry within the OnInit function, but unfortunately the other entries get deallocated once this function is done and the next function gets called. The counter in the OnTick function still says "2" as expected, but the first dequeue leads to an "invalid pointer access in 'test.mq5'" error. How can I fix this?
Cheers,
tandoori
The queue doesn't store "class instances", it stores pointers. Once you quit OnInit() your instances are out of scope and automatically destructed. The queue then stores invalid pointers.
CQueue can't store instances. You need to use dynamic objects and manage them (call delete when appropriate to avoid memory leaks).
EDIT : I didn't see William answer while posting.
This sounds like I have to use the new and delete keywords inside the Queue and Dequeue functions of the Queue class. Is this right? because Queue is a built-in extension of the mql5 language that I would like to avoid to touch.
Or is it just that I need to type out new and delete when creating and removing class instances like this?:
#include <Generic\Queue.mqh> class TestClass { public: ulong test_long; string test_string; // An initialisation function since public variables can not be initialised // properly otherwise. void Init() { test_long = 0; test_string = ""; }; // A function that prints out the content of the class instance. void PrintAll() { Print("TestClass content:"); Print("test_long: ", test_long); Print("test_string: ", test_string); }; }; CQueue<TestClass*> TESTCLASS; int OnInit() { TestClass* test_class1 = new TestClass(); TestClass* test_class2 = new TestClass(); test_class1.test_long = 1; test_class1.test_string = "a"; test_class2.test_long = 2; test_class2.test_string = "b"; // Put them in an order and see if it works. TESTCLASS.Enqueue(test_class1); TESTCLASS.Enqueue(test_class2); // Locally the content still exists. Print(TESTCLASS.Count()); TestClass* test_class = TESTCLASS.Dequeue(); test_class.PrintAll(); delete test_class; return INIT_SUCCEEDED; }; void OnTick() { // Globally it doesn't, but the counter still does. Print(TESTCLASS.Count()); TestClass* test_class = TESTCLASS.Dequeue(); test_class.PrintAll(); delete test_class; };
I mean I understand the basic scope formalism, but there are differences from language to language, so I'm a bit lost here.
Cheers,
tandoori
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hiya friends,
I have a problem with a CQueue that I want to use to store class objects. In one function, I put in class instances into the queue and in another function I want to take them out again. Locally inside the function in which I put them in I can dequeue without any problems, but globally these entries are gone. Here an example of an EA:
So as expected, we can dequeue and read out the first entry within the OnInit function, but unfortunately the other entries get deallocated once this function is done and the next function gets called. The counter in the OnTick function still says "2" as expected, but the first dequeue leads to an "invalid pointer access in 'test.mq5'" error. How can I fix this?
Cheers,
tandoori