-
Notifications
You must be signed in to change notification settings - Fork 588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing std::vector<Foo>& to a method when Foo does not have a public default constructor #464
Comments
It sounds like we don't need to create a |
ah, you're right :) that just works! wasn't really sure how to use the Pointer types. But, it does not work with the real scenario... though, so guess something else is going wrong there. What's happening is that the "limit" field of the Pointer does now grow. So seems like nothing is added to the vector, or the VectorAdapter does not pick it up some how. But maybe I should compare the generated c++ code for this example and the real scenario and see if I can understand what is going wrong. I guess you can close this issue then if you like :) |
Well, let me know if you can update it to reflect the real scenario. Thanks! |
Hi! I have modified my example now so that the generated jni methods in the example and the real lib now look the same (except for names). Also I added an operator=() to the Box class and i think that maybe I found something. Seems like the operator=() is called on an object that has been deleted or not initialized. Is that possible? At least the constructors are not called before = is called. When I did not have the operator=() the copy constructor was called instead. With the real lib the assignment operator is doing things that most often is causing an immediate segfault when called on a deleted object. Attached new example. |
|
Hi, added some more logging. It does not seem to be happening in the call to Could it be somewhere in the call to
After some reading I get the feeling that calling the copy assignment operator |
Or actually it might happen in the type conversion operator |
The generated code of VectorAdapter::operator P*() looks something like this:
And with this code I can reproduce the issue now without running javacpp. So I think the issue could be that memory is allocated but not initialized, then copy is called and it is using the copy assignment operator=(). So I guess one needs to understand if this is OK to do, and if it is then there is something wrong with the implementation of operator=() in the lib I am trying to call. |
Hi, again! :) In my case I think it would be safer to use an in place copy constructor instead:
That will avoid the operator=(). But maybe this is not general enough. |
@saudet Hi, this might actually solve the problem?
When I run this it is using the copy constructor instead of the assignment operator. |
Thanks again for the fix! It has been released with JavaCPP 1.5.5. |
Trying to generate jni wrappers for a c++ library that has the pattern of creating objects and pushing them into a std::vector that is passed in byref to a factory method. And the types do not have default constructors.
This is how it would look in c++
And to use the generated java it would look something like this
Problem is that the Box type does not have a public default constructor so does not seem to be possible to call this factory method using the generated java code.
I have attached a full example including c++ and java code.
javacpp_example.zip
The text was updated successfully, but these errors were encountered: