Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

How to Replay Custom Dynamic Structs (struct with vector)

Hi Forum,

i am currently developing a RecordReplay method to record a custom dynamic structure.

The structure looks like that:

struct {

char[64] header;

MAPSArray<uint16_t> data;


It works very good for recording.

When i am trying to implement the replay method, i read the recorded header, allocate buffer for the Array and read the data into the array.

All of this works well, except that it creates a huge memory leak, as it seems that the array buffer is not freed.

If i would write a standard component, i would inherit the MAPSCustomDynamicStructureComponent class as public.

How can i achieve freeing buffers with this setup?


Best Answer

  • TeoTeo
    Accepted Answer


    Ah yes, RRM can be tricky when we try to use custom structures !

    You could make it work with a dynamic custom struct, but you will have to manage the "lifecycle" of the struct. Have a look at this example :

    struct myData
    	myDataHeader header;
    	int allocated;
    	int used;
    	char* payload;
    	myData& operator=(const myData& data) {
    		if (&data != this) {
    			header = data.header;
    			if (data.allocated > allocated) { 
    				delete[] payload; 
    				if (data.allocated > 0)
    					payload = new char[data.allocated]; 
    				allocated = data.allocated; 
    			used = data.used;
    			if (data.used > 0)
    				MAPS::Memcpy(payload, data.payload, data.used);
    		return *this;
    	myData() : allocated(0), used(0), payload(NULL) {}
    	myData(const myData& data) { 
    		allocated = used = 0;
    		payload = NULL;
    		*this = data;
    	~myData() { delete[] payload; }

    So, if your struct is just a header and data as you posted, you could implement everything needed. But if that struct is going to evolve I would recommend going for a custom struct instead of a dynamic one. Actually, in general I would recommend using custom struct, it is easier to maintain and is enough in most cases.

    Just a few things to remember regarding the custom structure declaration itself :

    • You should disable structure fields alignment (force alignment to 1 byte) so the sizeof(MyNewStructure) will be the same whatever the platform and compiler. This will allow to use a generic record/replay method for all specific structures which can be used to record in binary format on one platform and replay on another.
    • No pointer are allowed in user-defined structures. So I'm afraid you cannot use MAPSArray within the structure. You need to use something with a fixed size, i.e
    #include "maps.hpp"
    #pragma pack(push,1)
    struct MyNewStructure 
        char[64] header;
        uint16_t[64] data; //The size is up to you and depends on your needs
    //Now re-enable structure alignment
    #pragma pack(pop)
    // The RTMaps input filter for the structure MyNewStructure
    const MAPSTypeFilterBase MAPSFilterMyNewStructure = MAPS_FILTER_USER_STRUCTURE(MyNewStructure);

    Let me know if this helps,

    Best regards,


  • Hi Teo,

    thank you very much for pointing out the "how to" in such a short time! That is very nice and definetely helps.

    Thanks for that tip with custom structures.

    The aim to use a dynamic custom struct was exactly to be independent of the data size. So i will have to implement methods and destructors within the structure definition.

    Great that it's working and the framework allows this.

    I really like that this is way faster than through the support.

    By the way: How did you do the listing?

  • One thing i'm stil curious of is: Why does it work within a CustomDynamicStructure Component? Is there something defined that cares about pointers?

  • Hello,

    I'm glad this helped !

    I cannot speak for the support (Intempora or dSPACE) but here on the forum we try to apply a FIFO reading policy regarding the discussions.

    Why does it work within a CustomDynamicStructure Component? 

    Classic custom struct were made first. But at one point we wanted to offer the option to use dynamic structures. However, in order to keep existing code working we did not edit the custom structure but instead created the dynamic structures.

    The main difference between the two is how the structure deals with memory, more specifically how it interacts with functions such as AllocOutputBuffer. And as mentioned before, you have to do some memory management yourself.

    Best regards,

Sign In or Register to comment.