Passing Julia function as an parameter to a wrapped C++ class

Hello, I’m trying to pass a Julia function as a callback function to a C++ class wrapped by CxxWrap. I’m not entirely sure how to go about it but here is what I have so far:

The C++ side:

#include<iostream>
#include "jlcxx/jlcxx.hpp"
#include <julia.h>

class derivedforJulia{
	public:
		//jl_init();

		jl_value_t *jlPtr;

		derivedforJulia(){
			jlPtr = NULL;
		}

		void setJlFnPtr(jl_value_t *x){
			jlPtr = x;
		}

		void reportName(){
			typedef void (*CppPtr)();
			CppPtr rePtr = reinterpret_cast<CppPtr>(jlPtr);
			rePtr();
		}

		~derivedforJulia(){}	

		//jl_atexit_hook(0);
};


JLCXX_MODULE define_julia_module(jlcxx::Module& cbmod)
{
  cbmod.add_type<derivedforJulia>("derivedforJulia")
  .constructor<>()
  .method("setJlFnPtr", &derivedforJulia::setJlFnPtr)
  .method("reportName", &derivedforJulia::reportName);
}

The Julia side:

module CppClass
  using CxxWrap
  @wrapmodule(joinpath("/home/hoangvu/Desktop/Duy/ClassTest/lib","libclasstestlib"))

  function __init__()
    @initcxx
  end
end

jlClass = CppClass.derivedforJulia()

jlFn() = println("hello")

jlPtr = @cfunction(jlFn, Cvoid, ())

CppClass.setJlFnPtr(jlClass,jlPtr)

CppClass.reportName(jlClass)

The C++ code compiled not problem so I think the problem is on the Julia side. This is the error I got:

ERROR: LoadError: ReadOnlyMemoryError()
Stacktrace:
 [1] reportName(::Main.CppClass.derivedforJuliaAllocated) at ./none:0
 [2] top-level scope at none:0
 [3] include at ./boot.jl:326 [inlined]
 [4] include_relative(::Module, ::String) at ./loading.jl:1038
 [5] include(::Module, ::String) at ./sysimg.jl:29
 [6] exec_options(::Base.JLOptions) at ./client.jl:267
 [7] _start() at ./client.jl:436
in expression starting at /home/hoangvu/Desktop/Duy/ClassTest/ClassTestJulia.jl:18

Does anyone knows how to go about doing this?

Thank you.

I have never used CxxWrap but if I have to guess, you should probably do something like

		void setJlFnPtr(jl_value_t *x){
			jlPtr = jl_unbox_voidpointer(x);
		}

At any rate a good start would be to print the value returned by @cfunction on the Julia side and the value of the pointer used in reportName.