Octave Memory Management

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Octave Memory Management

Thomas D. Dean-2
I have an .oct file that I want to open a device, return the pointer to
the device structure.

In octave:
octave> dev_ptr = oct_file("open");
octave> for idx = 1:10
           dat = oct)file()
         endfor
octave> rc = oct_file("close", dev_prt)

In the .cc file,

   OCTAVE_LOCAL_BUFFER(unit32_t, dev_ptr, 1);
   open(&dev_ptr,...)
   if (arg == "open") {
     return dev_ptr;
   }

   if (arg == "close") {
     close(dev_ptr);
   }

   OCTAVE_LOCAL_BUFFER (uint8_t, dat_buf, num_bytes );
   read_device(&dat_buf)

Does octave keep the allocation of dev_ptr between function calls?

Is it possible to use a static variable in the .cc file?  Would I have
to free it when the device is closed?

I can post the full application if necessary.

Tom Dean


Reply | Threaded
Open this post in threaded view
|

Re: Octave Memory Management

Sebastian Schöps
Thomas D. Dean-2 wrote

> I have an .oct file that I want to open a device, return the pointer to
> the device structure.
>
> ...
>
> Does octave keep the allocation of dev_ptr between function calls?
>
> Is it possible to use a static variable in the .cc file?  Would I have
> to free it when the device is closed?
>
> I can post the full application if necessary.
>
> Tom Dean

Maybe Carlo‘s old advice helps?
https://octave.1599824.n4.nabble.com/Persistent-dara-across-oct-files-tp4689039.html

Seb.
Seb.




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Octave Memory Management

Thomas D. Dean-2
On 8/18/19 11:36 AM, Sebastian Schöps wrote:

> Thomas D. Dean-2 wrote
>> I have an .oct file that I want to open a device, return the pointer to
>> the device structure.
>>
>> ...
>>
>> Does octave keep the allocation of dev_ptr between function calls?
>>
>> Is it possible to use a static variable in the .cc file?  Would I have
>> to free it when the device is closed?
>>
>> I can post the full application if necessary.
>>
>> Tom Dean
>
> Maybe Carlo‘s old advice helps?
> https://octave.1599824.n4.nabble.com/Persistent-dara-across-oct-files-tp4689039.html
>
> Seb.

Carlo's answer is static variables.

That gets me mostly there.  I am opening a USB device.

A clear("all") most likely destroys the instance of the static variable.
A subsequent call to the function results in
    usb_claim_interface error -6

This most likely results in a memory leak of the size of a device structure.

Is there any way to implement a "call on clear" or
"call on function removal" function.  Sort of like a device callback
function?  Google is not much help.

Tom Dean


Reply | Threaded
Open this post in threaded view
|

Re: Octave Memory Management

siko1056
On 8/19/19 6:32 AM, Thomas D. Dean wrote:

> On 8/18/19 11:36 AM, Sebastian Schöps wrote:
>> Thomas D. Dean-2 wrote
>>> I have an .oct file that I want to open a device, return the pointer to
>>> the device structure.
>>>
>>> ...
>>>
>>> Does octave keep the allocation of dev_ptr between function calls?
>>>
>>> Is it possible to use a static variable in the .cc file?  Would I have
>>> to free it when the device is closed?
>>>
>>> I can post the full application if necessary.
>>>
>>> Tom Dean
>>
>> Maybe Carlo‘s old advice helps?
>> https://octave.1599824.n4.nabble.com/Persistent-dara-across-oct-files-tp4689039.html
>>
>>
>> Seb.
>
> Carlo's answer is static variables.
>
> That gets me mostly there.  I am opening a USB device.
>
> A clear("all") most likely destroys the instance of the static variable.
> A subsequent call to the function results in
>    usb_claim_interface error -6
>
> This most likely results in a memory leak of the size of a device
> structure.
>
> Is there any way to implement a "call on clear" or
> "call on function removal" function.  Sort of like a device callback
> function?  Google is not much help.
>
> Tom Dean
>
>

Your description reminds me of the "onCleanup" function [1].  If you
wrap your oct-files in some m-file code, you should be able to define
some onCleanup action in case your wrapper object is cleared.

HTH,
Kai

[1] https://octave.org/doc/v5.1.0/XREFonCleanup.html


Reply | Threaded
Open this post in threaded view
|

Re: Octave Memory Management

Thomas D. Dean-2
On 8/18/19 3:30 PM, Kai Torben Ohlhus wrote:

> On 8/19/19 6:32 AM, Thomas D. Dean wrote:
>> On 8/18/19 11:36 AM, Sebastian Schöps wrote:
>>> Thomas D. Dean-2 wrote
>>>> I have an .oct file that I want to open a device, return the pointer to
>>>> the device structure.
>>>>
>>>> ...
>>>>
>>>> Does octave keep the allocation of dev_ptr between function calls?
>>>>
>>>> Is it possible to use a static variable in the .cc file?  Would I have
>>>> to free it when the device is closed?
>>>>
>>>> I can post the full application if necessary.
>>>>
>>>> Tom Dean
>>>
>>> Maybe Carlo‘s old advice helps?
>>> https://octave.1599824.n4.nabble.com/Persistent-dara-across-oct-files-tp4689039.html
>>>
>>>
>>> Seb.
>>
>> Carlo's answer is static variables.
>>
>> That gets me mostly there.  I am opening a USB device.
>>
>> A clear("all") most likely destroys the instance of the static variable.
>> A subsequent call to the function results in
>>     usb_claim_interface error -6
>>
>> This most likely results in a memory leak of the size of a device
>> structure.
>>
>> Is there any way to implement a "call on clear" or
>> "call on function removal" function.  Sort of like a device callback
>> function?  Google is not much help.
>>
>> Tom Dean
>>
>>
>
> Your description reminds me of the "onCleanup" function [1].  If you
> wrap your oct-files in some m-file code, you should be able to define
> some onCleanup action in case your wrapper object is cleared.
>
> HTH,
> Kai
>
> [1] https://octave.org/doc/v5.1.0/XREFonCleanup.html
>
>

Thanks, that fixes it.  Now, if I can only remember to do it...

Tom Dean