-
Notifications
You must be signed in to change notification settings - Fork 516
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
std::string inside of value types does not generate correct bindings for C# #1730
Comments
Fixing the generation was the easy part, however, there seems to be something wrong with how the strings work. After writing once you get the pleasure of continuously reading garbage. Not entirely sure how to tackle this one atm. |
Thanks for looking into this, might be worth getting the generation fixes in at least. Can't really justify spending time trying outside that myself atm fixing this, maybe we just merge your work and ignore the test for now. Can we easily generate code to throw an exception for this broken case? |
Found the issue: CppSharp does not generate a default constructor for value types, even if one is explicitly defined on the C++ side. This causes the string to not be initialized correctly, causing everything to break down later on. Providing a dummy empty constructor taking an int and calling it in C# makes everything work as expected. I'm not entirely sure what the best course of action here is, since you can't force ctors to be called on C# structs (
Either way, I think this calls for a new issue, after the codegen here is fixed. |
Good find.
This sounds ok to me, either initializing it or just throwing if the struct is not initialized. I'd rather not add more options as we already have quite a lot of them, but if the overhead is something anyone particularly cares about I'd be ok with it. Probably not necessary tho? |
Hello, I have a C++ class that I want to expose to C# as a value type using
CS_VALUE_TYPE
macro.However, I have noticed that if my C++ class contains
std::string
orconst char*
member, the generated code is not correct and cannot build. For example, consider the following C++ code:It generates
public unsafe partial struct ValueType
C# structure with the following code for member variables:StringMember
setter fails with "CS0212 You can only take the address of an unfixed expression inside of a fixed statement initializer" error message, whileCharPtrMember
setter fails with "CS0212 The name '___instance' does not exist in the current context".Do you know what could be the issue and how to fix it?
P.S.
The generated code works correctly if
CS_VALUE_TYPE
macro is omitted, but unfortunately, I need to use C# structs.Here is an example that works:
The text was updated successfully, but these errors were encountered: