@safe leak fix?
    Frank Benoit 
    keinfarbton at googlemail.com
       
    Wed Nov 11 15:59:16 PST 2009
    
    
  
Walter Bright schrieb:
> Consider the code:
> 
>   @safe:
>     T[] foo(T[] a) { return a; }
> 
>     T[] bar()
>     {
>         T[10] x;
>         return foo(x);
>     }
> 
If D would have something like a slice-info which could be returned
instead of the slice itself, then foo would be safe.
slice-info would be something like a struct/Tuple storing the start and
end index.
That applied onto the original array gives the slice.
SliceInfo foo( T[] a){
  // do something, resulting in e.g. a[2..6]
  return SliceInfo(2, 6);
}
T[] bar(){
	T[] x = new T[10];
	return x[foo(x)]; // safe compile OK
}
T[] bar(){
	T[10] x;
	return x[foo(x)]; // safe error, because x slice escapes
}
This shifts responsibility of memory safety to the caller with little
extra effort.
    
    
More information about the Digitalmars-d
mailing list