Moving a pointer up in pointerscope stack #724
Replies: 8 comments 34 replies
-
There's no way to do that in C++ so it's not something that we should need, but there's a couple of ways we can do something that like that. Either pass the outer scope to compute() and attach it that way before return, or call innerScope.extend() before return, and outerScope.attach() after return. |
Beta Was this translation helpful? Give feedback.
-
Why do you mean by that ? There is no C++ equivalent to
That would work, but
The workaround I use in such case is to allocate my result object before entering the inner scope, but that's not always possible or easy if I need to do some computations involving pointers to determine arguments needed for allocating the result object. So nothing essential, but I think it would be a nice addition to JavaCPP that would only add a small number of lines of code. |
Beta Was this translation helpful? Give feedback.
-
The idea is that there might be pointer scopes in the client code, and pointer scopes in the library scope. And the library code shouldn't mess up or assume anything about client code.
Just like it does:
and the resulting pointer should have the same lifespan on both case, depending if the client uses pointer scopes or not. If SomePointer compute() {
SomePointer result;
try (PointerScope ps1 = new PointerScope()) {
/* ... */
try (PointerScope ps2 = new PointerScope()) {
/* ... */
result = new SomePointer();
ps1.detachUp(result);
/* ... */
}
/* ... */
}
return result;
} Else we would need to detachUp twice: SomePointer compute() {
SomePointer result;
try (PointerScope ps1 = new PointerScope()) {
/* ... */
try (PointerScope ps2 = new PointerScope()) {
/* ... */
result = new SomePointer();
ps2.detachUp(result);
/* ... */
}
/* ... */
ps1.detachUp(result);
}
return result;
} |
Beta Was this translation helpful? Give feedback.
-
Back to this idea. I have written this function, which doesn't need to patch anything in JavaCPP: public static <P extends Pointer> P scopeEscape(P p) {
if (p == null) return null;
Iterator<PointerScope> it = PointerScope.getScopeIterator();
if (it == null) throw new IllegalStateException("Not in a pointer scope");
PointerScope inner = it.next();
if (it.hasNext()) {
it.next().attach(p);
}
inner.detach(p);
return p;
} And I'm heavily using it like this: Tensor forward(Tensor input) {
try (PointerScope ignored = new PointerScope()) {
/* Computations creating lots of Pointer subclasses */
return scopeEscape(result);
}
} Maybe can you think of something smarter ? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
We don't want the pointer to live forever either. Just to escape the current scope (avoid it for being deallocated when the scope exits).
I must be missing something. What will not work if we just register the pointer in the pointer stack of the inner scope when the pointer is created, and call |
Beta Was this translation helpful? Give feedback.
-
I see, attaching to multiple scopes will break. And we are back to your remark about "we can already to that, and the name of that method is PointerScope.attach(Pointer)." So is ref counting in pointer scopes only useful when the user manually attaches a pointer to some arbitrary scope ? |
Beta Was this translation helpful? Give feedback.
-
Such method would rather be |
Beta Was this translation helpful? Give feedback.
-
Say I have a method doing some complex computation in a
PointerScope
and producing a result as aPointer
. We can usePointerScope.detach(Pointer)
to prevent the result from being deallocated when we exit the scope:But what if the call to
compute
is itself within aPointerScope
?We would like the result to be deallocated when we exit this outper pointer scope.
So what about adding some method
PointerScope.detachUp(Pointer)
to move a pointer from an inner scope to the enclosing scope (if it exists, else simply detach) ?Or is there already a way to do it ?
Beta Was this translation helpful? Give feedback.
All reactions