Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to copy Pointer from a std::vector?

25 views
Skip to first unread message

equ...@gmail.com

unread,
May 24, 2021, 5:32:08 PM5/24/21
to javacpp
Hello.
I have an std::vector of C++ structs that I pass to Java via callback. I define it in InfoMap like this:

new Info("std::vector<Foo>").pointerTypes("FooVector").define()


As far as I understand, if I get a value from FooVector via get(n), it will not allocate a new instance of C++ Foo object (and call it copy constructor), instead Java Foo instance will be created with pointer referring to memory somewhere in the std::vector. Which means that vector can't be garbage collected while Foo is alive.
The problem is that I want Foo instances to be in independent memory locations after I passed vector to Java, and vector to be garbage collected and deallocated. How to achieve this in JavaCPP?

Samuel Audet

unread,
May 24, 2021, 9:52:10 PM5/24/21
to javacpp...@googlegroups.com, equ...@gmail.com
If you have control over the signature of the callback, you could simply
pass it by value, and JavaCPP will allocate a copy of it automatically.
If not, in the callback, we can allocate a new instance and copy the
content of the vector there.

On 5/25/21 6:32 AM, equ...@gmail.com wrote:
> Hello.
> I have an std::vector of C++ structs that I pass to Java via callback. I
> define it in InfoMap like this:
>
> new Info("std::vector<Foo>").pointerTypes("FooVector").define()
>
>
> As far as I understand, if I get a valuefrom FooVector via get(n), it

equ...@gmail.com

unread,
May 25, 2021, 4:53:22 PM5/25/21
to javacpp
>If you have control over the signature of the callback, you could simply
pass it by value, and JavaCPP will allocate a copy of it automatically.

I'm not sure what you mean. Are you talking about calling callback multiple time for each value instead of passing a vector into it? If so, I can't do that unfortunately.

>If not, in the callback, we can allocate a new instance and copy the
content of the vector there.

You mean a new instance of vector? I need values of Foo to separate, i.e. JVM have to be able to garbage collect them independently.

вторник, 25 мая 2021 г. в 04:52:10 UTC+3, Samuel Audet:

Samuel Audet

unread,
May 25, 2021, 8:02:56 PM5/25/21
to javacpp...@googlegroups.com, equ...@gmail.com
We can create independent instances of Foo, but that depends on whether Foo allows you to do that or not.

Could you maybe reformulate your question and provide more information about the constraints that you are facing?

equ...@gmail.com

unread,
Jun 2, 2021, 7:11:35 PM6/2/21
to javacpp
>Could you maybe reformulate your question and provide more information about the constraints that you are facing?

I need to pass vector of structs to Java using callback (pure virtual function).
On Java side, inside a callback I add Pointers of these structs to Java container. Later on, I remove them from container indepedently at different times and want them to be garbage collected and deallocated independently.

I decided to add template function that takes a reference and move constucts a new value out of it:

template<typename T>
inline T moveConstruct(T& other) { return std::move(other); }

And then define it for types I need using Info() and use it in Java when getting elements from vector. I'm not sure if it is the best solution with JavaCPP, but it works.

I also tried to use FooPointer with @StdVector instead of FooVector wrapper class (which creates a number of additional methods that I don't need) but discovered that it always creates new instance of my vector on heap before passing it to Java, which is unneccessary in my case (since I move values out of it immediately using moveConstruct). FooVector, on the other hand, passes pointer of existing vector if I pass it to callback as pointer or reference. Is there a way to do the same with @StdVector?
среда, 26 мая 2021 г. в 03:02:56 UTC+3, Samuel Audet:

Samuel Audet

unread,
Jun 2, 2021, 10:35:57 PM6/2/21
to javacpp...@googlegroups.com, equ...@gmail.com
Well, if Foo is just a POD struct, we can copy its content with, for example, new Foo.put(FooVector.get(i)) and that's it.

The StdVectorAdapter does have to copy the content, yes, that's normal.
If that's a problem, creating an instance of std::vector such as FooVector is the way to go.
Reply all
Reply to author
Forward
0 new messages