On Thursday, 16 September 2021 at 10:48:19 UTC, bauss wrote:
On Thursday, 16 September 2021 at 10:28:37 UTC, frame wrote:
I have C-code translated in D that acts sometimes incorrect if
the GC has made some collect. I would like to know why.
- Code runs correct if the GC collections are off
- There are no allocations within the C-translated-code except
`throw new` (but they are not called)
- All allocations made in C-translated-code are still
calloc/malloc `ed
- Even if I disable the GC before calling the function and
just enable it after there will be an incorrect result
- Data passed to the function belongs to a struct and the
function is called in a member function and is always correct
The main public function accepts a char* and returns a char*.
Signature is like this:
```d
char* fun(ref int, ref int, size_t, const char*, out int, out
int, out int, uint);
```
Input paramter gets the pointer from char[] `.ptr` property
(and length must be supplied too).
I didn't want to change the code much so I have some piece
like that:
```d
// const char *s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]^_";
char* s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]^_".dup.ptr;
```
Could this cause the issue? But the pointer is not used
outside the function where it's created.
Use toStringz and not .ptr.
Or append \0 to your string.
Also see the documentation for "toStringz" which has this:
```
Important Note: When passing a char* to a C function, and the C
function keeps it around for any reason, make sure that you keep
a reference to it in your D code. Otherwise, it may become
invalid during a garbage collection cycle and cause a nasty bug
when the C code tries to use it.
```
It probably should tell that somewhere else too.