Go Down

Topic: Memory corruption ? (Read 147 times) previous topic - next topic

o-zone

Hi all,

i spent past days trying to figure out how to correctly allocate memory of an object. Here's the code:

Code: [Select]

// REST Client
// https://github.com/michelep/esp8266-restclient
#include "RestClient.h" 

RestClient *restClient;


void setup() {
[...]
      restClient = new RestClient(config.collector_host);
[...]
      sprintf(query,"key=%s&ts=% [...]);

      statusCode = restClient->post("/rest/data/put", query, &response);
[...]
}


i think that restClient point to a non allocated memory space, however code compile correctly. But i don't know how to solve this issue. Someone can help me ?

Thanks, Michele

pylon

Don't allocate the objects yourself, let the compiler do it for you:

Code: [Select]

void setup() {
[...]
      RestClient restClient(config.collector_host);
[...]
      sprintf(query,"key=%s&ts=% [...]);

      statusCode = restClient.post("/rest/data/put", query, &response);
[...]
}


Does that run on a UNO?

o-zone

Thanks but i need to have restClient as a global var !

Deva_Rishi

so:
Code: [Select]

RestClient restClient(config.collector_host);

void setup() {
[...]
     
[...]
      sprintf(query,"key=%s&ts=% [...]);

      statusCode = restClient.post("/rest/data/put", query, &response);
[...]
}
To 'Correct' you have to be Correct. (and not be condescending..)

o-zone

Thanks but i cannot instantiate the object restClient on load because config.collector_host was set up after. So i need to create restClient *after* boot...

pylon

What type of Arduino are we talking about?

o-zone

it's a code for ESP8266

pylon

#7
Dec 07, 2018, 07:01 pm Last Edit: Dec 10, 2018, 06:17 pm by pylon
Which is not an Arduino.

How do you get the impression that the memory is not allocated?

Post complete code, the problem might be in another code part.

Deva_Rishi

Hi all,

i spent past days trying to figure out how to correctly allocate memory of an object. Here's the code:

Code: [Select]

// REST Client
// https://github.com/michelep/esp8266-restclient
#include "RestClient.h" 

RestClient *restClient;


void setup() {
[...]
      restClient = new RestClient(config.collector_host);
}


i think that restClient point to a non allocated memory space.
Actually i have no idea where it points to, but the memory for the pointer has been allocated !
during setup you allocate memory for the object itself which will be destroyed after setup() is completed. we keep the pointer, yahoo !
if you want restClient to be global you need to declare it as such, as before, Nowhere did you mention what kind of object 'config' is but if you need to declare that first you should do so.


To 'Correct' you have to be Correct. (and not be condescending..)

gfvalvo

during setup you allocate memory for the object itself which will be destroyed after setup() is completed. we keep the pointer, yahoo !
That is completely incorrect!!!

The 'new' operator in the 'setup()' function dynamically creates an object of the 'RestClient' class on the heap (i.e. it allocates memory and runs the constructor). It then assigns a pointer to this object to the global variable 'restClient' (which is of type 'RestClient *'). That object remains on the heap and valid until it is destroyed by the 'delete' operator. As stated, the pointer variable 'restClient' is global, so it remains valid and in-scope.

There is nothing wrong (at least syntactically or OPP-wise) with OP's code. He / she has just failed to provide any evidence that it does NOT work.


Go Up