Incorporating D

Rainer Schuetze r.sagitario at gmx.de
Sat Jan 26 02:52:56 PST 2013



On 26.01.2013 11:40, Johannes Pfau wrote:
> Yes, I just wanted to point out a common source for such bugs, it's not
> the GC's fault. It's great that the documentation of toStringz mentions
> that issue. What I meant is most of the time we use toStringz() like
> this:
>
> string str;
> c_function(str.toStringz());
>
> This is only valid if c_function doesn't store the pointer, but newbies
> might miss that and just copy this nice looking example code for other
> c functions. There's nothing we can do about that though, interfacing
> to C just is a little bit dangerous.
>

It is even dangerous if it is only used temporarily during that function 
call, but copied elsewhere in the C heap and cleared on the stack:

struct param_struct { const char* name; };

void c_function(const char*p)
{
	param_struct* ps = new param_struct;
	ps->name = p;
	p = 0;
	doSomething(ps);
	delete ps;
}

Imagine a garbage collection while executing doSomething...


More information about the Digitalmars-d mailing list