For the sake of arguments, lets discuss this code snippet:
void GetConfig(void) {
String RawData;
String s = SendAPI("config");
TJSONObject *config = new TJSONObject();
config->Parse(s.BytesOf(),0);
TJSONPair *pair = new TJSONPair("",config);
RawData = JSONToFormatted(pair,0);
delete pair;
delete config;
}
When I return from the function I get an access violation inside the VCL library trying to run the TJSONObject destructor.
If I remove the delete config statement, it runs fine. I suspect that the TJSONPair is taking ownership of the TJSONObject and frees it on the delete pair statement. Hence the config instance being invalid.
In another place I made a clone of the TJSONObject instance before creating the TJSONPair instance, this allowed me to continue access and free the original TJSONObject instance. Thus supports the hypothesis that TJSONPair claims ownership.
Can someone confirm this, or do I get a huge memory leak if I exclude the delete config statement?
Yes, the TJSONPair
takes ownership of the TJSONObject
and will free it for you.