Memory Management for `const char* const` return values: Who deallocates or what is the allocated block's lifetime?

There are a few APIs that return char* (or similar) pointers.

There are a number of other APIs that return char* type strings, but they don’t seem like the kind of thing that would frequently change size or value and could easily be members of objects that get allocated once and last the lifetime of the application. RapidCodeNetworkNode::NameGet() is an example of what I’m thinking of.

I suppose the the RMP runtime manages memory management for things like this. If that is the case, what is the lifetime of the allocated memory blocks? If I call the function again (from a different thread, e.g.) right away, what happens to the data/memory from the first call?

What expectations should I have for memory returned this way?

(At the moment, I’m specifically thinking about ServiceChannelRead(...) for arbitrary-length binary data, but I’m interested in the general answer as well.)

In general, the lifetimes of the allocated memory blocks pointed to by the returned const char* const pointers are tied to the lifetime of the RapidCodeObject they were returned by. The memory blocks are a single char buffer of a predetermined size (specific to each object type) tied to each instance, and will be deallocated when the RapidCodeObject’s destructor for that instance is called. However, because there is only a single char buffer per instance, subsequent calls may overwrite what is in that buffer. Each call to a RapidCodeObject’s method will lock the buffer so that only one read operation can occur at a time, but the client code will have to copy the value to a different location if it wishes to continue using that value. Simply keeping the pointer and using it to access the value may result in seeing different values upon subsequent retrievals.

Are you experiencing any issues with these methods?

I’m not having issues, per se. I was curious about the data returned from ServiceChannelReadString(), since it is of (perhaps) arbitrary length. Since the call requests the size, I wasn’t sure if this was being saved and returned in a static, preallocated buffer or something dynamically allocated.

Is there a maximum byteCount that an SDO read will return in RMP?

ServiceChannelReadString() will return up to 1024 bytes.
NetworkLogMessageGet() will return up to 256 bytes.
EncoderRatioPrimaryGet() will return up to 25 bytes.

1 Like