You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
embind: Val call invokers delete object arguments #21016
This is a regression from #20383. Before the patch the destructor of objects passed into a val functions were not automatically run, but now they are. While this is a regression, I think it's actually just copying the strange(broken?) behavior of the other code. There was another issue filed about this as #20095 related to val::call which used the optimized code that val::operator() now uses.
I did a bit of digging and it appears that automatically destroying the object arguments was the originally intended behavior in this patch and that the user should call clone to avoid this.
I'm still thinking about the best approach here, but my initial thought is that the user should handle explicitly deleting the object if they need to.
Output before:
>>> constructor
>>> before callback
branch [object Object]
>>> after callback
>>> destructor
After:
>>> constructor
>>> before callback
branch [object Object]
>>> destructor
>>> after callback
>>> destructor
#include<stdio.h>
#include<emscripten.h>
#include<emscripten/bind.h>usingnamespaceemscripten;structBranch {
Branch() {
printf(">>> constructor\n");
}
~Branch() {
printf(">>> destructor\n");
}
};
classEditor {
public:voidWithBranch(std::function<void(Branch*)> callback) {
Branch b;
printf(">>> before callback\n");
callback(&b); // This pointer should not be deleted!printf(">>> after callback\n");
}
};
voidEditorWithBranch(Editor& editor, val callback) {
editor.WithBranch([callback](Branch* branch) { callback(branch); });
}
intmain() {
EM_ASM(
var editor = new Module.Editor();
editor.withBranch((branch) => {
console.log("branch " + branch);
});
);
}
EMSCRIPTEN_BINDINGS(xxx) {
class_<Editor>("Editor").constructor<>().function("withBranch", &EditorWithBranch);
class_<Branch>("Branch");
}
Activity
brendandahl commentedon Jan 4, 2024
cc @RReverser
RReverser commentedon Jan 5, 2024
Hm this is a tricky one, especially because there's no way to express "pass by reference" vs "pass by value" in dynamic JS calls...
I tend to agree that passing by reference by default and leaving destructors to C++ is likely the better choice though.
embind: Don't automatically destroy arguments passed into val calls.
embind: Don't automatically destroy arguments passed into val calls.
embind: Don't automatically destroy arguments passed into val calls.
embind: Don't automatically destroy arguments passed into val calls. (#…