Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 598

 
Juer:

Can you tell me if I'm creating a CArrayObj object and adding other objects to it? I still need the objects themselves. And I can delete this list object.

Which method should I use to remove objects from it before deleting? Detach() or Delete()?

Or Clear() / Shutdown() ?

The confusion is because you don't understand how memory is allocated and how it should be handled. In general case neither CArrayObj nor objects located in it, There is no need to delete manually.

// Где-то в коде есть объект CArrayObj
CArrayObj ArrayOfItems;
...
//Где-то в коде заполнили коллекцию пользовательскими объектами
void Create()
{
  ArrayOfItems.Add(new CUserItem());
  ArrayOfItems.Add(new CUserItem());
  //...
  ArrayOfItems.Add(new CUserItem());
}
// Так не правильно делать. Удалять ничего не надо.
void OnDeinit(const int reason)
{
   delete ArrayOfItems;
}
 

I pass an object to the class function, a reference to it, so that the actions in the function will happen to it, and then it will be returned. But I don't need the class object itself anymore... So, how do I delete it?

for(int i=0;i<test_objects.Total();i++)
  {
   bool res=false;
   CTestObject *test_object=test_objects.At(i);
   if(!CheckPointer(test_object)
        continue;
   CTestClass *test_class=new CTestClass();
   if(test_class.Check(test_object))
        res=true;
   delete test_class;
  }

Like this. When I delete test_class, it says Invalid pointer at the next iteration...

How can I delete test_class without deleting the object passed to it?

 
Vasiliy Sokolov:

You are confused because you don't understand how memory is allocated and how it should be handled. In the general case, neither CArrayObj nor objects inside it , there is no need to delete manually.

Well, if I have created an array of objects. That is, I created an array object ArrayObj, performed necessary actions with it. Then I don't need this object, but need the objects themselves inside it (array members), shouldn't I delete this array object?

 

This vv Ihor Herasko will make it to the weekend, and it's not there ; )

questions have been pending since last thursday! : )

i'm responsible for my reviews after all, and i gave you a good review, be nice, end your game a la "i answer 1 question a day", because i'm not excited about waiting, and i don't really want to proofread my review. at this rate i'll have to wait all next week, and i need to give the owl to a freelancer for revision ; )
 
Juer:

I pass an object to the class function, a reference to it, so that the actions in the function will happen to it, and then it will be returned. But I don't need the class object itself anymore... So, how do I delete it?

Like this. When I delete test_class, it says Invalid pointer at the next iteration...

How do I delete test_class without deleting the object passed to it?

You need to avoid creating pointers to objects. Work with objects directly on the function stack instead:

CTestClass test_class;
for(int i=0;i<test_objects.Total();i++)
  {
   bool res=false;
   CTestObject *test_object=dynamic_cast<CTestObject*>(test_objects.At(i));
   if(test_object == NULL)
        continue;
   if(test_class.Check(test_object))
        res=true;
  }

Since test_object is guaranteed to contain CTestObjects (you haven't added anything besides these objects, right?:) Then you can simplify code by removing the ghost check:

CTestClass test_class;
for(int i=0;i<test_objects.Total();i++)
  {
   CTestObject *test_object = test_objects.At(i);
   bool res = test_class.Check(test_object);   
  }

And just briefly:

CTestClass test_class;
for(int i=0;i<test_objects.Total();i++)
   bool res = test_class.Check((CTestObject)test_objects.At(i));
 
Juer:

Well, if I have created an array of objects. That is, I created an array object ArrayObj, performed necessary actions with it. Then I don't need this object, but I need the objects themselves inside (array members), shouldn't I delete this array object?

You don't understand the purpose of CArrayObj. It is the owner of the objects you place in it. If you remove CArrayObj, the objects you put in it will also be removed. Since ArrayObj is the owner of these objects, it is also their manager - it controls the references to objects and automatically deletes all allocated memory when the collection is no longer needed. So you don't need to free anything and use the delete operator. Everything is done by CArrayObj itself. The CArrayObj itself takes almost no memory in the computer (a few tens of bytes), so it is pointless to try to delete it.

 
Vasiliy Sokolov:

You do not understand the purpose of CArrayObj. It is the owner of the objects you place in it. If you remove CArrayObj, then the objects you put into it will also be removed. Since ArrayObj is the owner of these objects, it is also their manager - it controls the references to objects and automatically deletes all allocated memory when the collection is no longer needed. So you don't need to free anything and use the delete operator. Everything is done by CArrayObj itself. The CArrayObj itself takes almost no memory in the computer (a few tens of bytes), so there's no point in trying to delete it.

But then errors undeleted objects CArrayObj

 
Juer:

But then errors undeleted objects CArrayObj

So they made a mistake with CArrayObj. They have written CArrayObj* test_objects instead of CArrayObj, but forgot to make a constructor for it; perhaps they misconfigured the memory model to handle CArrayObj. Maybe programmers simply overdid it with pointers. The diagnosis is clear: using pointers where they are not needed often leads to leaks.

 

Hello!

Can you tell me how to move the cursor to, say, line 7 in a text document, to extract a substring from this position...

Which is faster - multiple text files with one line or one file with more lines?

 
Vasiliy Sokolov:

So they made a mistake with CArrayObj. Instead of: CArrayObj test_objects; They wrote: CArrayObj* test_objects, but forgot to make a destructor for it; Maybe they incorrectly configured memory model to work with CArrayObj. Maybe they simply overdid it with pointers. The diagnosis is clear: using pointers where they are not needed often causes leaks.

But where are they needed at all? What's the point of using them if you can just declare objects?