bytedeco/javacpp
View on GitHubVirtualizing a class causes a method returning a String return a dangling pointer
Open
#656 opened on Feb 25, 2023
enhancementhelp wanted
Repository metrics
- Stars
- (4,279 stars)
- PR merge metrics
- (No merged PRs in 30d)
Description
The JNI code for method name of Module in Pytorch contains:
signed char* rptr;
StringAdapter< char > radapter(ptr->name());
rptr = radapter;
If we add an Info virtualize() on Module, the code becomes:
const signed char* rptr;
StringAdapter< char > radapter((dynamic_cast<JavaCPP_torch_0003a_0003ann_0003a_0003aModule*>(ptr) != NULL ? ((JavaCPP_torch_0003a_0003ann_0003a_0003aModule*)ptr)->name() : ptr->name()));
rptr = radapter;
The const modifier of rptr, when present or not, changes which overload of the implicit cast on the last line is applied. When absent, a copy of the string is performed. When present, no copy is performed and the returned pointer is dangling because the string it points to is freed at the end of the block.
Probably related to #374