I am trying to port fmusdk for learning purposes. I already have something that 
compiles, now need to actually make it work.

Coming from python and not programming very often in Nim, I tend to struggle 
with memory related stuff.

My first question goes as follows:

The `fmusdk` works on an instance of the following 
[object](https://github.com/mantielero/fmi.nim/blob/1b894e2e5464c032147b95d3e79bd8d4560ea54a/src/modelinstancetype.nim#L13):
 
    
    
    type
      ModelInstance* = object
        r*:          ptr UncheckedArray[fmi2Real]#(NUMBER_OF_REALS)
        i*:          ptr UncheckedArray[fmi2Integer]#(NUMBER_OF_INTEGERS)
        b*:          ptr UncheckedArray[fmi2Boolean]#(NUMBER_OF_BOOLEANS)
        s*:          ptr UncheckedArray[fmi2String] #(NUMBER_OF_STRINGS)
        isPositive*: ptr 
UncheckedArray[fmi2Boolean]#(NUMBER_OF_EVENT_INDICATORS)
        time*: fmi2Real
        instanceName*: fmi2String
        `type`*: fmi2Type
        GUID*: fmi2String
        functions*: ptr fmi2CallbackFunctions
        loggingOn*: fmi2Boolean
        logCategories*: array[0..NUMBER_OF_CATEGORIES-1, fmi2Boolean]
        componentEnvironment*: fmi2ComponentEnvironment
        state*: ModelState
        eventInfo*: fmi2EventInfo
        isDirtyValues*: fmi2Boolean
        isNewEventIteration*: fmi2Boolean
    
    
    Run

which contains a number of varaibles and functions.

The first function that get called is 
[fmi2Instantiate](https://github.com/mantielero/fmi.nim/blob/1b894e2e5464c032147b95d3e79bd8d4560ea54a/src/modelinstance.nim#L42).
 This function returns a pointer to the object in C. I am 
[returning](https://github.com/mantielero/fmi.nim/blob/1b894e2e5464c032147b95d3e79bd8d4560ea54a/src/modelinstance.nim#L180):
    
    
    return unsafeAddr( comp )
    
    
    Run

> I understand that the data get garbage collected because the following 
> function shows garbage when accessing the object fields.

I have tried `return addr(comp)` (no success). Now (not in github) I am trying 
`return comp` with the following signature: 
    
    
    proc fmi2Instantiate*( instanceName: fmi2String, fmuType: fmi2Type,
                          fmuGUID: fmi2String, fmuResourceLocation: fmi2String,
                          functions: ptr fmi2CallbackFunctions, visible: 
fmi2Boolean,
                          loggingOn: fmi2Boolean): ModelInstance =
    
    
    Run

So I am a bit lost.

Bearing in mind that then I apply `{.exportc:"$1", cdecl, dynlib.}` to all 
these functions. What would be the rigtht way of passing the instance that is 
created in `fmi2Instantiate` so that others functions can operate on its values.

Right now I was using in other functions something like this: `comp: var 
ModelInstance`
    
    
    proc fmi2SetupExperiment*(comp: var ModelInstance; toleranceDefined: 
fmi2Boolean;
                             tolerance: fmi2Real; startTime: fmi2Real;
                             stopTimeDefined: fmi2Boolean; stopTime: fmi2Real): 
fmi2Status =
    
    
    Run

Thanks for you support

Reply via email to