Comments (12)
You're going to have the same problem in C++ if you try to create a std::unique_ptr from an object allocated on the stack. That's how you should think about this. Although we can't actually allocate objects on the stack, that's how it behaves, to make the Java syntax as close as possible to C++.
from javacpp.
A C++ equivalent is:
#include "inc.h"
#include <memory>
#include <stdio.h>
int main(int ac, char **av) {
std::unique_ptr<G> gp = std::make_unique<G>();
printf("%d\n", gp->i);
C c(std::move(gp));
printf("%p\n", gp.get());
printf("%d\n", c.get());
}
I don't think there is something wrong. Is it ? It prints the correct:
18
(nil)
18
Management of object G
is passed to object C
and it shouldn't be destroyed as long as object C
lives.
For a real world example, see in PyTorch OptimizerParamGroup
and the options
argument of its constructor.
Currently my attempts to use it from the presets trigger crashes.
from javacpp.
That's not equivalent. If you call std::make_unique with JavaCPP it's going to do the same thing and it will work.
from javacpp.
Can I ? Using similar trick that we did with @Shared
annotation on constructors ?
I'm wondering if, in the UniquePtrAdapter
, we shouldn't empty the owner
field when we perform the move
in U&&()
casting operator.
from javacpp.
We could make it so that std::make_unique gets called as part of the constructor of the Java object, sure...
from javacpp.
Ok, Adding @UniquePtr @Name("std::make_unique<G>")
to constructors of classes that can be instantiated in Java and passed to native function as unique pointer seems mandatory indeed, like for shared pointers.
However, I still think there is a problem of owner field not cleared after a move:
std::unique_ptr<G> x() { return std::make_unique<G>(); }
void y(std::unique_ptr<G> g) { }
G g = x();
y(g);
g.deallocate();
Triggers a segmentation fault (double free).
I don't think it's normal. The owner field of g
should have been cleared at the same time of its address. Don't you think so ?
from javacpp.
The owner field is fine, that's the std::unique_ptr itself. If it still crashes it's something else
from javacpp.
The owner field of UniquePtrAdapter
is the managed pointer owner if I understand well.
As shown in the /* XXX */
line above of the JNI code, this field is used to reinitialize the owner field of the Java object after
the method call. So after a move we end up with a Java object with a null address but a not-null owner, which is deallocated
when the Java object is GCed.
Replacing in the adapter:
operator U&&() { return UNIQUE_PTR_NAMESPACE::move(uniquePtr); }
by
operator U&&() { owner = NULL; return UNIQUE_PTR_NAMESPACE::move(uniquePtr); }
seems to fix the problem, but I don't really know if that makes sense.
from javacpp.
Hum, I think that's going to create a memory leak somewhere, but the existing unit tests for UniquePtrAdapter pass, so... Open a pull request if you're sure you want this, it's fine by me
from javacpp.
Oh, no, actually this can't create a memory leak because we can't modify the deallocator anyway, so that's going to get deallocated fine.
from javacpp.
if you're sure you want this, it's fine by me
I'm sure there is a double free problem somewhere, but not sure at all it's the right fix.
Or whether a similar fix would be needed somewhere: probably in the same cast operator overload in MoveAdapter
?
I'll also push a PR for Pytorch to add the proper annotations on constructors of OptimizerOptions
and its subclasses, and probably others. Several other presets are potentially affected: arrow, onnxruntime,onnx, tensorflow-lite, tritonserver.
from javacpp.
I'm sure there is a double free problem somewhere, but not sure at all it's the right fix. Or whether a similar fix would be needed somewhere: probably in the same cast operator overload in
MoveAdapter
?
Well it looks fine to me. The pointer is dead anyway after that function call so it can't hurt. Same for MoveAdapter, sure, thanks
I'll also push a PR for Pytorch to add the proper annotations on constructors of
OptimizerOptions
and its subclasses, and probably others. Several other presets are potentially affected: arrow, onnxruntime,onnx, tensorflow-lite, tritonserver.
👍 Don't bother with Arrow though, I'm not maintaining that one
from javacpp.
Related Issues (20)
- Mapping Template Constraints, Parser Error HOT 5
- Parser cannot compile because of QT HOT 4
- Template class with template functions HOT 15
- Inadequate Error Handling HOT 1
- `@StdVector` default annotation causing `Parser` to ignore templated constructors and operator overloads with `std::vector` arguments HOT 10
- Potential class loading deadlock when call static methods of Loader or Pointer HOT 13
- Multiple pointer types and automatic generation of function overloads HOT 1
- Parser doesn't add @Virtual annotations HOT 2
- A question about garbage collection on native objects HOT 3
- Pointer types don't work on the native side if allocated in java HOT 16
- Missing dependencies reported by gradle HOT 1
- Not found libs based on x86 in 1.5.10 HOT 4
- Marking an interface with virtualize() causes compilation failure when returning byte array
- Panama HOT 1
- Class instance state is reset between calls HOT 5
- Duplicate Constructor and allocate HOT 4
- Question related to std::function HOT 7
- Skip #include from a header file HOT 2
- java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "allInherited" is null HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javacpp.