Add C conversion constructors and fix new leak#356
Conversation
|
Yes, I test pool*array and found string, dictionary also be modified, so I test them. Use String will not leak. Array and Dictionay will leak. Now use your fix code, Dictionary and Array is ok. |
|
@akien-mga can you merge this pull request |
|
This seems to fix the problem, at least the leak warnings on GDNative/Godot shutdown disappear: Code used to reproduce
var instance = A.new() # A is a custom GDNative class
var array = [
B.new() # B is another GDNative class
]
instance.test_arrays(array)void A::test_arrays(godot::Array arr)
{
godot::Ref<B> elem = arr[0];
} // RAII should clean up hereRegarding exposing C types in the C++ API: if this is a problem, you can add the two |
|
I wonder if Edit: it does not. It's an invalid cast. As for making the operators friend functions and the constructors private: that's a perfect idea, thanks! I forgot about That worked for Dictionary, but Array.hpp needs to forward-declare Variant instead of including its header. I don't know the structure of the headers well enough to reorganize them properly, so I used |
10c3d94 to
1b988b4
Compare
|
Any feedback on the last few changes? I'm happy to merge this in. |
include/core/Array.hpp
Outdated
| godot_array _godot_array; | ||
|
|
||
| friend class Variant; | ||
| inline Array(const godot_array other) { |
There was a problem hiding this comment.
Two little things:
- You might want to make the constructor
explicit - Parameter should probably be a const-reference (even if
sizeof(godot_array)is small in current implementation)
Same for the Dictionary constructor.
There was a problem hiding this comment.
Done, thanks again for the improvements.
1b988b4 to
66c671b
Compare
|
Thanks! |
#355 fixed a leak caused by copy constructors. However, for Array and Dictionary, the fix now calls default constructors that allocate memory for an empty array/dictionary, again resulting in a leak. The other types fixed by #355 aren't affected - their default constructors do nothing.
The only way I can think of to convert a
godot_arrayto agodot::Arraywithout calling either constructor is to add a newArray(godot_array a)constructor to do the conversion. This way is clean and safe, but exposes part of the C API to C++. If we want to avoid that, maybe we can find a better way.