# Garbage collection of Julia code in C++

**URL:** <https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894>\
**Category:** General Usage\
**Tags:** embedding\
**Created:** [May 14, 2018, 9:43pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894 "2018-05-14T21:43:28Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 14, 2018, 9:43pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/1 "2018-05-14T21:43:28Z")

</div>

Hello everyone,  
I have a trivial question regarding the garbage collection for the c api of Julia. I am currently embedding Julia in a C++ class, and I need to access different mutable structs that I create in the C++ class constructor throughout all of its methods. To do so, I store pointers to jl\_value\_t\* inside the class, and assign them to Julia objects in the constructor, sort of like this:

```julia
class Component
{
public:
	Component()
	{
		jl_init();

		/* some code to include functions from an external .jl file.... */

		jl_function_t* mutable_struct_constructor = jl_get_function(jl_current_module, "MyMutableStruct");

		/* should I worry about the fact that pointer_to_mutable struct, that now holds a pointer to an allocated
		mutable struct, can be deleted and not used in other methods of this class? */

		pointer_to_mutable_struct = jl_call0(mutable_struct_constructor);
	}

	~Component()
	{
		jl_atexit_hook(0);
	}

	void uselessFunction()
	{
		/* Can I use the mutable struct with other functions in here? It looks to be working in my code, but does it break? */
	}
private:
	jl_value_t* pointer_to_mutable_struct;
};

```

I was thinking of a workaround, which is to make the allocated mutable struct a global variable, like so:

```julia
class Component
{
public:
	Component()
	{
		jl_init();

		/* some code to include functions from an external .jl file.... */

		jl_function_t* mutable_struct_constructor = jl_get_function(jl_current_module, "MyMutableStruct");

		/* should I worry about the fact that pointer_to_mutable struct, that now holds a pointer to an allocated
		mutable struct, can be deleted and not used in other methods of this class? */

		jl_set_global(jl_current_module, jl_symbol("MutableStruct"), mutable_struct_constructor);
		pointer_to_mutable_struct = jl_get_global(jl_symbol("MutableStruct"));
	}

	~Component()
	{
		jl_atexit_hook(0);
	}

	void uselessFunction()
	{
		/* This seems to work aswell. */
	}
private:
	jl_value_t* pointer_to_mutable_struct;
};

```

Would the JL\_GC\_PUSH and POP macros work across the class? If I were to push the variable in the constructor and popping it in the destructor? I feel like it wouldn’t.  
My question then is: what would be the best way to make sure that objects are not deleted by the GC? Also, if I were to disable the GC, how could I delete some allocated code (e.g., the data that a jl\_value\_t\* is pointing at) by myself? Sorry if these are trivial questions!

Thanks

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 15, 2018, 12:47am UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/2 "2018-05-15T00:47:51Z")

</div>

> [@vitreo12](#):
>
> jl\_init()

> [@vitreo12](#):
>
> jl\_atexit\_hook(0)

Make sure you don’t actually do this unless there will only ever be one `Component` constructed during the whole lifetime of the program.

> [@vitreo12](#):
>
> should I worry about the fact that pointer\_to\_mutable struct, that now holds a pointer to an allocated  
> mutable struct, can be deleted and not used in other methods of this class?

Yes.

> [@vitreo12](#):
>
> Would the JL\_GC\_PUSH and POP macros work across the class?

No.

> [@vitreo12](#):
>
> if I were to disable the GC

Don’t.

In general, the lifeness of an object has to be bound to either a local scope or global variables. Additionally, the bound could be indirect through parent object(s).

If the object you want to keep alive is not directly global or easily tied to a scope, you need to store it in a parent object during the time you want to keep it alive. You can add or remove the reference to your object from this parent object to realize the lifetime you want. Such an object can be anything that you can store your references in and unless you have some special requirement you could just use an `ObjectIdDict`.

Now the only question left is how do you keep this parent object alive. Unless you want to do something fancy you should just keep it alive either in a global (store it to a global variable in julia) or a local scope (`JL_GC_PUSH`…). A global variable is more general while a local frame could be faster. Which one of these to use depends on your usecase.

---

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 15, 2018, 10:11am UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/3 "2018-05-15T10:11:54Z")

</div>

> In general, the lifeness of an object has to be bound to either a local scope or global variables. Additionally, the bound could be indirect through parent object(s).

> If the object you want to keep alive is not directly global or easily tied to a scope, you need to store it in a parent object during the time you want to keep it alive. You can add or remove the reference to your object from this parent object to realize the lifetime you want. Such an object can be anything that you can store your references in and unless you have some special requirement you could just use an ObjectIdDict.

> Now the only question left is how do you keep this parent object alive. Unless you want to do something fancy you should just keep it alive either in a global (store it to a global variable in julia) or a local scope (JL\_GC\_PUSH…). A global variable is more general while a local frame could be faster. Which one of these to use depends on your usecase.

What if the scope of the mutable struct is the C++ class, with all its methods. How should I go about it? Should I make the allocated mutable struct object global, as I have done here? :

```julia
jl_set_global(jl_current_module, jl_symbol("MutableStruct"), mutable_struct_constructor);
pointer_to_mutable_struct = jl_get_global(jl_symbol("MutableStruct"));

```

Also, what should I do to deallocate it, or to let know the GC that the object can be freed. Should I do something like this in the destructor of my C++ class:

```julia
jl_set_global(jl_current_module, jl_symbol("MutableStruct"), nullptr);
pointer_to_mutable_struct = nullptr;

```

Would this approach work to dynamically handle what I want the GC to collect or not? Would there be more elegant ways? Thanks again

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 15, 2018, 12:18pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/4 "2018-05-15T12:18:56Z")

</div>

… I thought the decision process I gave was pretty unambiguous…

> [@vitreo12](#):
>
> Should I make the allocated mutable struct object global, as I have done here? :

It works if you won’t create multiple objects at the same time. (Also, you don’t need the `jl_get_global`.) If you want to create more than one objects at the same time, you should use what I mentioned above.

---

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 15, 2018, 1:09pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/5 "2018-05-15T13:09:48Z")

</div>

> … I thought the decision process I gave was pretty unambiguous…

I am sorry that I didn’t understand what you meant. Looking back at it, would this make sense then?  
In the private space of the C++ class:

```julia
std::vector<uintptr_t> this_object_table_of_references;

```

In the constructor:

```julia
//create object
pointer_to_mutable_struct = jl_call0(jl_get_function(jl_current_module, "MyMutableStruct"));

//add its id to the vector
this_object_table_of_references.push_back(jl_object_id(pointer_to_mutable_struct));

//add other eventual objects to the vector.....
//
//

```

In the destructor:

```julia
this_object_table_of_references.clear();

```

Sorry again for my misunderstanding, I am slowly learning how to use Julia and how to link it to my C++ projects.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 15, 2018, 1:28pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/6 "2018-05-15T13:28:55Z")

</div>

No julia’s GC can only scan julia objects. Therefore, you need to store the reference in a julia object. From the way you describe things and that you are from C++, I’m guessing you’ve never used a tracing GC. You may want to have a look at [Tracing garbage collection - Wikipedia](https://en.wikipedia.org/wiki/Tracing_garbage_collection) (only the first part which should be pretty easy to understand) to know what a tracing GC is. You don’t need to know all the detail but knowing how it find all live objects will hopefully make what I said make more sense.

Other than having to use a julia object, what you have is roughtly correct. The few other differences from what I said is that you should probably use a hash table (`ObjectIdDict` is one, `unordered_map` in c++) or a sorted data structure if you don’t want a linear scan in the destructor to pop it. You probably also don’t want to simply `clear` it since it’ll remove other objects from it too.

I couldn’t tell if you have this right but another thing you might have got wrong is that the table need to be rooted either in a c++ local scope or as a global variable. (It must not be a c++ non-static member but something managed externally.) If there are multiple objects you want to keep alive that belongs to the same though you can obviously store then in an Julia container that’s private to the c++ object and root this container globally so it’s easier to pop it out of the global dict all at once.

---

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 15, 2018, 2:33pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/7 "2018-05-15T14:33:47Z")

</div>

> Other than having to use a julia object, what you have is roughtly correct. The few other differences from what I said is that you should probably use a hash table (ObjectIdDict is one, unordered\_map in c++) or a sorted data structure if you don’t want a linear scan in the destructor to pop it. You probably also don’t want to simply clear it since it’ll remove other objects from it too.

Thank you very much for your help and the explanations, it is much clearer now in my head! I think I got it working, is that so? :

```julia
class Component
{
public:
	Component()
	{
		object_id_dict = jl_call0(jl_get_function(jl_current_module, "ObjectIdDict"));
        jl_function_t* add_index_to_dict = jl_get_function(jl_current_module, "setindex!");

		pointer_to_mutable_struct = jl_call0(jl_get_function(jl_current_module, "MyMutableStruct"));

		//add the object to the references of the ObjectIdDict

		jl_call3(add_index_to_dict, object_id_dict, pointer_to_mutable_struct, pointer_to_mutable_struct);
	}

	~Component()
	{
		//remove all the references from the ObjectIdDict of this class
		jl_function_t* empty_object_id_dict = jl_get_function(jl_current_module, "empty!");
        jl_call1(empty_object_id_dict, object_id_dict);
	}

	/* ... all functions that use pointer_to_mutable_struct here... */

private:
	jl_value_t* object_id_dict;
	jl_value_t* pointer_to_mutable_struct;

};

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 15, 2018, 5:09pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/8 "2018-05-15T17:09:55Z")

</div>

Actually no…

Creating an object, `ObjectIdDict` or others, will **NOT** make julia keep it alive. You still need to store it either as a global variable or as a local root.

---

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 15, 2018, 7:04pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/9 "2018-05-15T19:04:50Z")

</div>

> [@yuyichao](#):
>
> Creating an object, `ObjectIdDict` or others, will **NOT** make julia keep it alive. You still need to store it either as a global variable or as a local root.

So, if I only store the ObjectIdDict as a global variable, would it also keep the objects it is pointing to alive? Or would they need to be global aswell? Also, how could I go for the local root approach?Do you have any hints about it? Sorry if I am being annoying.  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 15, 2018, 7:20pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/10 "2018-05-15T19:20:52Z")

</div>

> [@vitreo12](#):
>
> if I only store the ObjectIdDict as a global variable, would it also keep the objects it is pointing to alive?

Yes.

> [@vitreo12](#):
>
> how could I go for the local root approach

It’s the JL\_GC\_PUSH/POP macros. They can only be used within the same C++ scope.

Also note that you still need to answer,

1. do you want to create more than one object? (If yes, you should not create new object and store it to the same global variable each time since they will overwrite each other)
2. do you need to keep alive multiple julia object per c++ object? (If not, you shouldn’t create a c++ object local julia container)

---

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 15, 2018, 7:55pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/11 "2018-05-15T19:55:17Z")

</div>

Thanks again. It is so much clearer now. Should I worry about setting the global variable that is associated with my OrderIdDict to a nullptr before exiting the code? Or would that be taken care of in the final cleanup of the Julia GC at jl\_atexit\_hook() anyway?

> [@yuyichao](#):
>
> Also note that you still need to answer,
> 
> 1. do you want to create more than one object? (If yes, you should not create new object and store it to the same global variable each time since they will overwrite each other)
> 2. do you need to keep alive multiple julia object per c++ object? (If not, you shouldn’t create a c++ object local julia container)

I still have to figure out how would my code work, I am just prototyping and learning the API at the moment.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 15, 2018, 8:05pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/12 "2018-05-15T20:05:40Z")

</div>

> [@vitreo12](#):
>
> Should I worry about setting the global variable that is associated with my OrderIdDict to a nullptr before exiting the code?

That should not be needed. In fact, unsetting the variable could be dangerous (there’s no julia code that can do this so the compiler makes assumptions that it cannot happen). If you are really paranoid you can just set that global variable to `nothing`.

---

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 15, 2018, 8:50pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/13 "2018-05-15T20:50:58Z")

</div>

I have two last questions:

1. Could I make a ObjectIdDict of ObjectIdDicts? Considering that I may have some parent class which needs to know about the ObjectIdDicts of several child classes.
2. To remove a reference of an object from a global ObjectIdDict, should I just delete the entry with the `delete!` method? Would this then make the object that I was referencing to available for the GC, when it feels like collecting? Would this approach also work if I were to remove an eventual child ObjectIdDict entry (and all the objects’ references that it was storing) from a global parent one?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 15, 2018, 10:08pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/14 "2018-05-15T22:08:51Z")

</div>

> [@vitreo12](#):
>
> Could I make a ObjectIdDict of ObjectIdDicts? Considering that I may have some parent class which needs to know about the ObjectIdDicts of several child classes.

`ObjectIdDict` is just a very basic hash table. You don’t need to use it unless you need to push and pop from it randomly. It can obviously hold any objects (though if that’s your question you should probably familiarize yourself with containers in julia).

> [@vitreo12](#):
>
> To remove a reference of an object from a global ObjectIdDict, should I just delete the entry with the `delete!` method?

Yes. And you can even just store `nothing` as the value, which is likely better in every ways. You don’t need an `ObjectIdDict` for each object unless you want to constantly popping from it. (An array would do just fine and much faster if you are just keep pushing to it).

> [@vitreo12](#):
>
> Would this then make the object that I was referencing to available for the GC, when it feels like collecting? Would this approach also work if I were to remove an eventual child ObjectIdDict entry (and all the objects’ references that it was storing) from a global parent one?

There might have been a bug causing the values in the keys or the values of an ObjectIdDict to be not free’d that’s recently fixed. Other than that, any objects unreachable are dead for the GC so they will be free’d when the GC feels like it assuming there’s no other references to them.

---

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 15, 2018, 10:44pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/15 "2018-05-15T22:44:04Z")

</div>

> [@yuyichao](#):
>
> You don’t need an `ObjectIdDict` for each object unless you want to constantly popping from it. (An array would do just fine and much faster if you are just keep pushing to it).

Would it then also make sense to create a global `Array{Ref}` to store all the references to my Julia objects? Would the array keep them alive as long as the reference inside the array exists?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 15, 2018, 11:57pm UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/16 "2018-05-15T23:57:16Z")

</div>

> [@vitreo12](#):
>
> `Array{Ref}`

Don’t. You use `Array{Any}` (or more accurately `Vector{Any}`). It’s fine but will be hard to pop. As mentioned above, it is a good choice if you don’t need to pop.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [May 16, 2018, 10:33am UTC](https://discourse.julialang.org/t/garbage-collection-of-julia-code-in-c/10894/17 "2018-05-16T10:33:19Z")

</div>

There are also functions in CxxWrap.jl for protecting/unprotecting:

[https://github.com/JuliaInterop/libcxxwrap-julia/blob/master/include/jlcxx/type\_conversion.hpp#L50-L108](https://github.com/JuliaInterop/libcxxwrap-julia/blob/master/include/jlcxx/type_conversion.hpp#L50-L108)

Note that if you use this directly, the array keeping the objects alive will live in the CxxWrap module, maybe I should change that behavior now the C++ part of the package is a separate library.
