These are chat archives for symengine/symengine

12th
Jan 2015
Isuru Fernando
@isuruf
Jan 12 2015 01:39
Yeah, once the garbage collector kicks in, the memory is freed. (or run gc() to call it manually)
Peter Brady
@pbrady
Jan 12 2015 17:47
@isuruf this is very cool! For reference, here's the julia docs on garbage collection http://julia.readthedocs.org/en/latest/manual/calling-c-and-fortran-code/#garbage-collection-safety. My only concern here is that both c++ and julia may try to free the same memory. I'm not sure if this is a valid concern since I haven't spent too much time looking into julia finalizers.
Isuru Fernando
@isuruf
Jan 12 2015 18:21
@pbrady, I think that's why we can't register a finalizer for a Ptr{Void}. Here the finalizer is registered on the Julia type Basic which when goes out of scope and garbage collection is done, calls the finalizing function which in turn calls the C function to free the C struct
Peter Brady
@pbrady
Jan 12 2015 18:27
It looks like the way you have implemented it is the 'approved' way http://stackoverflow.com/questions/21400550/julia-ptrvoid-finalizer-error. I'm trying to wrap my head around what's going on here. Does this sound correct:
  1. gc triggers the call to the finalizer
  1. the finalizer handles the data pointed to by the julia void ptr in Basic by calling the c++ basic_free. So the result of this is a julia type (Basic) with a void ptr set to NULL (or something like that).
  1. The julia type Basic is then deallocated by the julia gc
Isuru Fernando
@isuruf
Jan 12 2015 18:34
Yeah, when c++ basic_free is called the julia type is a void ptr called a dangling pointer
Peter Brady
@pbrady
Jan 12 2015 18:38
Great. It seems like all the memory management is nicely handled then. I was poking around for automatically generating julia interfaces from c headers and found https://github.com/ihnorton/Clang.jl. I'm not sure if it will suit our needs but It would be nice to be able to generate things automatically.
Isuru Fernando
@isuruf
Jan 12 2015 18:42
Yeah, that will be helpful given the fact that at the moment, a C++ function has a wrapper in Python, C and Julia.
Ondřej Čertík
@certik
Jan 12 2015 22:34
I think I figured out how to make the C wrappers as fast as an equivalent C++ code here: sympy/csympy#366
It's still hackish and we need to figure out some way to simplify it, but I think the idea is correct. No CWrapper struct, no extra heap allocations anymore.
Ondřej Čertík
@certik
Jan 12 2015 22:39
We need to benchmark it, but if there is truly no performance hit with the C wrappers, then we can use them for the Python wrappers as well. That way, the C interface would get a lot more testing. Right now the Python wrappers use Cython which calls C++ directly, and there are some nasty casts.