Corrupt data when using C++ binding from C#
I'm using CppSharp to generate a binding for very complex C++ API, to use it from C#. This complex API involves calling a method with a big and deep structure. C++ code should populate structure with data and return it to me. The structure is 13384 size.
Now, what I was doing is next (C#):
var cfgParams = new __cfg_params(); // The structure generated by CppSharp
var cfgErrors = new __cfg_errors();
ErrorCodeCfg(cfg_dll.cfg_import_settings(cfgParams, cfgErrors));
// Here I read some (but only some) of the fields of cfgParams as random garbage (about 40% of the time)
But if I do this:
var cfgParamsT2 = __cfg_params.__CreateInstance(new __cfg_params.__Internal());
var cfgErrorsT2 = new __cfg_errors();
ErrorCodeCfg(cfg_dll.cfg_import_settings(cfgParamsT2, cfgErrorsT2));
It start to be working as expected and I receive proper data in cfgParamsT2
Unfortunately cannot provide .h files I'm generated code from, because it is proprietary code (Dialog Production Line Tool), so I can only hope somebody has some ideas/is aware of the issue. This might also be not an issue if CreateInstance is really supposed to be used here, and I should not use constructors, but in that case it makes sense to generate them as private?
Do you by any chance generate __cfg_params as a struct rather than a class (by marking it explicitly)? If you do, please remove it. We do not handle empty constructors of structs correctly yet.
The generated __cfg_params is a class, when it's __Internal is a struct. I do not mark anything explicitly, I actually use all defaults for CppSharp. The C corresponding structure of __cfg_params is a struct
I understand you may not share your header but I am then afraid this is about everything we can do. If you can replicate the problem with a made up, and possibly simplified, header, I'd be happy to take a look.
I will see what I can do, but I'm pretty sure I won't be able to reproduce it with simple example. My header files are very very complicated, and this is probably why it starts behaving weird.
Anyway I'm fine with using .CreateInstance(new Internal()) calls, it seems to work 100% of the cases.
I'm not closing the ticket in case somebody (or me) finds a simpler code example
Maybe you can send us (privately if you want) the relevant pieces of the generated code, that way we might be able to figure out what's going on. This seems like an important bug to get fixed.
(Removed comments with examples) Guys, I'm sorry, it looks like the debugger just showed the wrong value. I think the issue is there - I've seen it, but my example is wrong. I had no idea you cannot trust the debugger in VS. I will keep working and if I happen at the issue again - I will post it here
Are the libraries you're trying to bind C or C++ code?
I think there might be an issue with C code in that AllocHGlobal never zero-initializes the allocated memory and why sometimes garbage shows up. This has never been a problem with C++ code because we usually call a native constructor which initializes the memory.
You are right, it is C, not C++, sorry for the confusion. Any known fix to this?
Our options contain a setting for the language, please use a C value there.
Yeah, I was going to mention, can you try setting:
driver.ParserOptions.LanguageVersion = Parser.LanguageVersion.C;
And see if that changes any of the struct offsets in the generated code?
About the other AllocHGlobal issue, we'll have to fix it and release a new version.
@archeg In addition, could I ask a favour of you? Please use the regular constructor instead of __CreateInstance but initialise each field yourself. Using default values such as 0 or null is fine, just so that it's initialised similar to what a programmer in C would do. Then please run your code again and tell us if you still get garbage in your data.
~~Just tried to switch to C, received a lot of errors Cannot implicitly convert type 'bool' to 'sbyte' (and back). I will try to fix them manually tomorrow, and report the results.~~ Nevermind, this is not CppSharp issue. It has generated all the properties as sbyte when previously they were bool
@ddobrev I think I did this when I reported the issue, - I was initializing all the fields manually. I was still receiving garbage, but it was occasional and somehow dependent on the run (I had app runs where everything was ok, despite a lot of structures were used). If I had a faulty run, garbage was always the same, - not random, and the same fields. But I will try it again tomorrow, so to be sure
@ddobrev I have tried to return to invoking constructors, and initializing all the fields. The data I receive on C end is with garbage (but only part of the structure). Furthermore, even if I pass this initialized structure to fill to C code (this C code can read the structure from a file, but this method requires a pointer to existing empty structure in memory), the data I receive is still corrupt. This doesn't happen when I use CreateInstance (at least in this particular case). So I assume C cannot write to this memory as well.
I've done this test with CppSharp set to parse C++, because if I switch to C, suddenly generated public constructors are gone, and I am only left with CreateInstance as an option.
From time to time I also receive AccessViolation in the property setter of the binding generated by CppSharp. I think I also have seen this even with CreateInstance, and both C/C++ parser options, but I still want to check that again.
Just to give you some insight, here is the initialization code of a single structure I use: https://gist.github.com/archeg/cc7bfaefbdcd0b55d97b65f02d8c50c2 I had to generate that, it is impossible to write that by hand.
Also I think you are aware of that, but I wanted to mention. It is not possible to write code like this:
A.B.C = 5
Because the getter of B makes a copy. Not sure this is a bug or intended, but it was not obvious to me
@archeg thank you for your tests. We now have some idea about what improvements can make but unfortunately we do not know precisely what the problem is. I am afraid we cannot help any further without a complete test case. I do think, though, that I can offer you an alternative. Our main page has a support e-mail which, should you decide to, you could use to send us your headers. I can guarantee their privacy. Please also note that we only need your headers, not your implementation (.c files).
@ddobrev Thanks a lot for the proposal, much appreciate that. It is not a problem to send over our source code, but the issue is that .c and .h files are something we use but do not own. I can contact the owner of ip for that, but it is a bit complicated and it's not my decision. We have an option to write custom C wrapper that has a simpler API, and use CppSharp then, which will probably work ok (btw, CppSharp looks really great, and definitely the best tool I've seen, everything else does much worse). I think we should go this way, because I'm already out of planned time for the implementation, but if I find some more free time later, I could come back to the issue