Skip to content
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

Meta: Proper and friendly constructor/destructor support #244

Open
1 of 19 tasks
PathogenDavid opened this issue May 6, 2022 · 0 comments
Open
1 of 19 tasks

Meta: Proper and friendly constructor/destructor support #244

PathogenDavid opened this issue May 6, 2022 · 0 comments
Labels
Area-OutputGeneration Issues concerning the process of generating output from Biohazrd Area-Translation Issues concerning the translation from libclang into Biohazrd Blocks-OpenCV Blocks-PhysX Concept-Correctness Issues concerning the correctness of the translation from an ABI perspective Concept-CppFeatures Issues concerning unsupported C++ features Concept-CrossPlatform Issues concerning cross-platform support Concept-OutputFriendliness Issues concerning the friendliness of using the Biohazrd output Concept-OutputUsability Issues concerning whether or not the output from Biohazrd is actually usable Platform-Linux Issues specific to Linux Platform-Windows Issues specific to Windows TechDebt

Comments

@PathogenDavid
Copy link
Member

PathogenDavid commented May 6, 2022

Up until now I've mostly ignored object construction/destruction because most libraries we've used with Biohazrd thus far have primarily had object lifetimes managed by the target library. (IE: Things are allocated and deallocated exclusively in native code.)

This isn't sustainable long-term, as many libraries (and even some aspects of libraries we already wrap) expect you to manage object lifetimes yourself.

At a high level, we need to support the following:

  • Create an analyzer to error when calling new() or using default(T) when it would bypass the constructor. (These should be separate diagnostics.)
  • Allow allocating C++ objects on the managed stack. (Just works™)
  • Allow destroying C++ objects on the managed stack. (Currently broken)
  • Allow allocating C++ objects on the native heap.
    • At a glance it looks like Microsoft and Itanium both implement this by calling operator new to allocate memory and then separately call the constructor.
  • Allow deallocating C++ objects on the native heap.
  • Allow allocating C++ objects extended in C# on some heap.
    • Right now the Mochi.PhysX snippets have a Pinned<T> helper which basically lets you do this on the managed heap. Unfortunately .NET limitations mean T cannot contain managed references. (You can only really pin managed objects via an interior pointer and can't allocate them on the pinned heap.)
  • Allow deallocating C++ objects extended in C# from some heap.
    • Most notably this needs to support calling the base destructor.

It would also be nice if we could find a way to properly manage the above situations using IDisposable. It's tricky in C# since we don't intrinsically know how the object was allocated like we do in C++. Dispose()/using makes sense for stack-allocated objects, but less so for heap-allocated ones. We don't want to accidentally mislead people into thinking they can/should call Dispose() on heap-allocated objects and cause the destructor to be called without actually freeing memory. (Although if we don't permit allocating on the native heap -- IE: Only allow something like Pinned<T> this becomes a non-issue I think?)

Since C++ objects expect automatic destructor calls for stack-allocated objects, it might also be helpful to implement an analyzer to tell people to use using for objects allocated on the stack from C#. It's pretty easy to accidentally miss this when porting code from C++ since it's implicit there.

Additionally we need to investigate how virtual destructors affect any or all of the above.


The follow issues relate to this effort:

See also #163

@PathogenDavid PathogenDavid added Concept-Correctness Issues concerning the correctness of the translation from an ABI perspective Concept-CppFeatures Issues concerning unsupported C++ features Area-Translation Issues concerning the translation from libclang into Biohazrd Area-OutputGeneration Issues concerning the process of generating output from Biohazrd Concept-OutputUsability Issues concerning whether or not the output from Biohazrd is actually usable Concept-CrossPlatform Issues concerning cross-platform support Concept-OutputFriendliness Issues concerning the friendliness of using the Biohazrd output Blocks-OpenCV Platform-Linux Issues specific to Linux Platform-Windows Issues specific to Windows TechDebt Blocks-PhysX labels May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-OutputGeneration Issues concerning the process of generating output from Biohazrd Area-Translation Issues concerning the translation from libclang into Biohazrd Blocks-OpenCV Blocks-PhysX Concept-Correctness Issues concerning the correctness of the translation from an ABI perspective Concept-CppFeatures Issues concerning unsupported C++ features Concept-CrossPlatform Issues concerning cross-platform support Concept-OutputFriendliness Issues concerning the friendliness of using the Biohazrd output Concept-OutputUsability Issues concerning whether or not the output from Biohazrd is actually usable Platform-Linux Issues specific to Linux Platform-Windows Issues specific to Windows TechDebt
Projects
None yet
Development

No branches or pull requests

1 participant