This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTSC destruct() and delete()

Other Parts Discussed in Thread: SYSBIOS

Hi,

When an application is finished working with a dynamically-created instance of some module, does calling Mod_delete(someHandle) deallocate the memory associated with the someHandle instance?  From the documentation, I see that delete() sets the handle to NULL but it is not described whether the memory is released for reuse. 

Also, if I want to construct instances in some variable MyMod_Struct myInstStruct, is it necessary to call destruct() before calling construct() a second time?  Or will construct() just overwrite existing values in myInstStruct?

Thanks,

Nick

  • Nick,

    Any memory allocated in the Mod_create is freed in the Mod_delete including the object/nstance itself.

    Yes, you should call destruct before you call construct. In the construct, the module might have allocated memory. The destruct will free that memory. If you did construct back to back, there would be a memory leak in this case.

    Todd

  • One more question:

    I've got a module, ThisModule, with the following instance struct:

    struct Instance_State
    {
       Int                                          Type;   
       IMyInterface.Handle          MyInterfaceH;
       IOtherInterface.Handle     OtherH;
    }

    The underlying IMyInterface module implementation has @InstanceFinalize since that module features handles to other modules.  However, I did not declare the IOtherInterface implementation with @InstanceFinalize since it only has simple types (Int, etc).

    In the Instance_finalize function for ThisModule, should I call IOtherInterface_delete(obj->OtherH) or will that memory be taken care of automatically when calling ThisModule_delete()?

  • The rule of thumb is...if you created, constructed or allocated something in ThisModule's instance_init, you need to delete, destruct or free it in ThisModule's finalize.

    Todd

  • Ok, thanks.  I asked because I was having problems calling the delete() function for the module without @InstanceFinalize.  However, I think the error was because delete() was getting called on a static instance, not a dynamic instance.  The error was a failed assert in HeapMem:

    [C64XP_0] ti.sysbios.heaps.HeapMem: line 331: assertion failure: A_invalidFree: Invalid free
    [C64XP_0] xdc.runtime.Error.raise: terminating execution

    Not sure if there's a way to differentiate between handles to static and dynamic instances....  Or maybe a way to copy the parameters of a static instance to a dynamic instance so that I won't have to check such things?

  • Nick,

    It looks like HeapMem caught the fact that the memory being freed was not allocated from its heap. That would happen if you try to free a static object (or static anything).

    For SYS/BIOS we decided not to check if the instance was static or dynamic in the delete. We just state that you should not delete a static instance. We opted to not increase the footprint of the code and potentially object to catch this. If you want you could add a field in your object state (e.g. Bool staticCreate) that can be used to check in your module. One question would be what to do if you catch it since most of the delete code is generated for you. You could assert in the module...or just let HeapMem assert with a invalidFree:)

    Todd

  • Adding an internal field is actually what I was thinking last night.  I'm planning to create some ENUM who's value is either STATIC or DYNAMIC and then check its value in Instance_finalize() for ThisModule before calling delete() on OtherH and MyInterfaceH.

    Thanks for your help!