Questions on OOP in MQL5 - page 72

 

I have some misunderstanding with pointers in MQL5 - a very "unspecified thing".

I ran into a problem with a test script (I have the library attached):

this code will delete the pointer, everything is OK.

#include <JSON\json.mqh>

JSONObject * getJSONObject(const string json)
{
   JSONParser  parser;
   JSONValue*  jv;
   JSONObject* jo = jv = ((JSONParser)(parser)).parse(json);
   if (jv != NULL && jv.isObject()) return(jo);
   Print(__FUNCSIG__ + "parser error, json = ",json);
   delete jv; 
   return(NULL);
}
void OnStart()
{
   JSONObject *jobj = getJSONObject("{\"ObjType\":2,\"m_period\":1}");
   if(jobj!=NULL) Print("m_period = ", jobj.getInt("m_period"));
   delete jobj; 
}

I wanted to write it this way at first:

#include <JSON\json.mqh>

JSONObject * getJSONObject(const string json)
{
   JSONValue *jv =((JSONParser *)(new JSONParser())).parse(json);
   if (jv != NULL && jv.isObject()) return((JSONObject *)jv);
   Print(__FUNCSIG__ + "parser error, json = ",json);
   delete jv; 
   return(NULL);
}
void OnStart()
{
   JSONObject *jobj = getJSONObject("{\"ObjType\":2,\"m_period\":1}");
   if(jobj!=NULL) Print("m_period = ", jobj.getInt("m_period"));
   delete jobj; 
}

when running the script, I get: 1 object of type JSONParser left


so how do i remove this JSONParser outside of the function ?


Files:
Include.zip  12 kb
 
Igor Makanu:

I have some misunderstanding with pointers in MQL5 - a very "unspecified thing".

I ran into a problem with a test script (I have attached the library):

this code will delete the pointer, everything is OK.

I wanted to write it this way at first:

when running the script, I get: 1 object of type JSONParser left


so how do i remove this JSONParser outside of the function ?


A temporary object must be created on the stack in this case)))
CSomeObj(<params>).SomeMethod()
 
Vladimir Simakov:
You have to create a temporary object on the stack in this case)))
CSomeObj(<params>).SomeMethod()

this is my first example, I create JSONParser in local scope there, it will be deleted when I exit the function

The question is, basically, that such things as an example #2 Sharp will swallow, and here I still need to think... in general Sharp is more succinct than Pros!!! - so for a cholivar! )))

 
Igor Makanu:

this is my first example, I create JSONParser in local scope there, it will be deleted when I exit the function

the question is, in principle, that such things as an example number 2 Sharpe will swallow, and here you still need to think ... in general Sharp is more succinct than Pros!!! - so for a cholivar! )))

You create a pointer to an object in a local area, but the object itself is in the heap and that's what leaks).
It's not a Sharp, the rubbish collector is missing)))
 
Vladimir Simakov:
You create a pointer to an object in a local area and the object itself is in a heap and it's the object that leaks).
It's not sharpe, there's no rubbish collector)))

I told you Sharpe rules! )))

Nn, in general, the first option I will use and will not bother the brain, thank you for participating!

 

I don't understand the behaviour of pointers in MQL anyway, this code works without any problems and will delete all dynamically created objects at the end of the script:

#include <JSON\json.mqh>
//+------------------------------------------------------------------+
JSONObject *getJSONObject(const string json)
{
   JSONParser *parser = new JSONParser();
   JSONValue *jv = parser.parse(json);
   delete parser;
   if (jv != NULL && jv.isObject()) return((JSONObject *)jv);
   Print(__FUNCSIG__ + "parser error, json = ", json);
   delete jv;
   return(NULL);
}
//+------------------------------------------------------------------+
void OnStart()
{
   JSONObject *jobj = getJSONObject("{\"ObjType\":2,\"m_period\":1}");
   if(jobj != NULL) Print("m_period = ", jobj.getInt("m_period"));
   delete jobj;
}


why does it work? i have deleted the JSONParser *parser object, so it should get all related pointers as NULL

 
Igor Makanu:

I don't understand the behaviour of pointers in MQL anyway, this code works without any problems and will delete all dynamically created objects at the end of the script:


why does it work? i just deleted JSONParser *parser object, so it should get all pointers as NULL

why ?

Again, this isn't Sharp, there's no such thing as "linked references"... The object lives until the programmer personally kills it, regardless of references.

 
Maxim Kuznetsov:

why would I do that?

again this isn't sharpe, there's no such thing as "linked references"... The object lives until the programmer kills it personally, regardless of references.

Well, that's the question, we have a workable variant of the code, but the pointers' behavior is unclear to me

In theory, variant 2 from the first message should work

 
It's hilarious for users who get into discussions about "high" when in practice they can't put 1+1 together.
I wonder if the Dunning-Krueger effect is stinging anywhere?
 

how cheeky I am here...

you got a knowledge discount, I didn't get a discount) even though they're all expensive

Reason: