0xC0000005: 0x0000000000000000 access violation...
Simen Kjærås
simen.kjaras at gmail.com
Mon Jul 2 07:49:07 UTC 2018
On Sunday, 1 July 2018 at 22:04:05 UTC, Robert M. Münch wrote:
> On 2018-07-01 09:05:56 +0000, Robert M. Münch said:
>
>> This one look nasty...
>
> And it was... the problem was, that I kept D allocated pointers
> in C code without informing the GC that the memory can't be
> collected nor moved. Bridging from D to C and back, is pretty
> tricky to not miss any allocated memory.
>
> Couldn't such a problem be mitigated with an annotation like
> @externalRef or so, which would add the code to protect such
> memory from being GC? It would make the code much more explicit.
An annotation probably won't do it. This might:
struct ExternalRef(T) {
T* ptr;
alias ptr this;
void free() {
import core.stdc.stdlib : free;
free(ptr);
}
}
auto externalRef(T)() {
import core.stdc.stdlib : malloc;
return ExternalRef!T(cast(T*)malloc(T.sizeof));
}
extern(C)
void sendToC(ExternalRef!int p);
extern(C)
ExternalRef!int getFromC();
unittest {
sendToC(externalRef!int());
getFromC().free();
}
This is hardly a production-quality piece of code, but should
give some ideas for what's possible.
--
Simen
More information about the Digitalmars-d-learn
mailing list