Matthias247 has quit [Read error: Connection reset by peer]
jmiven has quit [Quit: co'o]
jmiven has joined #ponylang
jemc has joined #ponylang
amclain has quit [Quit: Leaving]
ro6 has quit [Quit: Connection closed for inactivity]
jemc has quit [Ping timeout: 240 seconds]
puzza007 has quit [Quit: Connection closed for inactivity]
flippant has quit [Ping timeout: 240 seconds]
SparkySparkyBoom has joined #ponylang
rurban has joined #ponylang
prettyvanilla has quit [Ping timeout: 255 seconds]
_andre has joined #ponylang
Candle has joined #ponylang
<kempe>
Hello! I've been interested in pony for a while and was thinking of trying it out for a project I have in mind. One quirk is that I have to pass binary data to and from functions written in C (I'm hooking some custom networking related kernel things) and I'm wondering if there is a best practice for doing this. I saw the the Array type has a function called from_cpointer, but I assume that memory management
<kempe>
would have to be done on the C side of things since it states it doesn't copy the data?
<SeanTAllen>
if the memory is allocated by c, then yes, by default you would need to free it yourself
<kempe>
Is there some way to copy the data returned to pony so that I can use a local stack variable in C and let that one fall out of scope and let pony's GC handle the memory management on the pony side? I'd rather avoid manual memory management as much as possible
<SeanTAllen>
yes but you will still need to free the original memory
<kempe>
Although that would be done automatically if I don't use malloc, but a stack variable local to the function in C I'm calling, right?
<kempe>
When the function ends and the variable goes out of scope
<SeanTAllen>
if you wanted an array for example, i would suggest checking out the "from_cpointer" constructor that is part of Array
<kempe>
I did have a look at that one, but since it states that the data is not copied I assume I can't use a local variable C side since the variable the pointer is reffering too can't go out of scope until the pony code is done with it? That is I'd have to use malloc when getting the data and make sure to have a C function calling free on the memory when pony's GC reclaims the Array. Or does pony automatically
<kempe>
free the memory of the pointer used in from_cpointer when the Array is GC'ed?
<SeanTAllen>
right in the case of from_cpointer then you wouldnt be freeing that memory, it would be handled for you
<SeanTAllen>
actually im not sure of that
<kempe>
Nice! Then that works perfectly. Thank you!
<SeanTAllen>
don't trust me on that. i'm under the weather today.
<kempe>
No worries, at least I have a starting point. I'll play around a bit and see how it works out
<kempe>
I must say I am crushing a bit on pony at the moment :P
<SeanTAllen>
we have a product that has a C++ API and memory is allocated in C++ but we need to free at some point from pony
<SeanTAllen>
we create a CPPData object to wrap that memory and when it is freed, we have a finalizer on CPPData that will free the underlying memory
<kempe>
Okay, I see. I guess I might have to wrap the array in another object that calls a free function in C from its final member function
<SeanTAllen>
it's what we do so the person using the C++ API doesn't have to worry about when the memory needs to be freed
<kempe>
Okay, sounds reasonable
<SeanTAllen>
One of our requirements for our C++ API was "end developer shouldnt have to manually manage memory"
<kempe>
A wise requirement as most crashes, in my experience, are caused by memory handling errors
<SeanTAllen>
another option, kempe, if you have control of the c code is to use pony_alloc to allocate the memory.
<SeanTAllen>
if you want to use "from_cpointer", you can use that then clone the array, the free the C pointer using FFI
wizeman has joined #ponylang
<kempe>
I'm not familiar with pony_alloc. Should I read the source for pony or is it documented somewhere?