DIP1000

Loara loara at noreply.com
Tue Jun 28 21:40:44 UTC 2022


On Friday, 24 June 2022 at 18:31:14 UTC, Ola Fosheim Grøstad 
wrote:
> The objects are in the calling function, not in the connect() 
> function. So they are not destroyed when the connect() function 
> returns.

When `connect()` returns may happen that `b` is destroyed but `a` 
not, so `a.next` contains a dangling pointer that will bring 
potential segmentation faults that could be detected only with 
tools like Valgrind, just consider

```d
node * calling(return scope node * a) @safe{
   scope node *b = new node(); //b is stack allocated
   connect(a, b);
   return a;
   //b destroyed but a not
}
```

The `scope` attribute tries to avoid these events preventing you 
from doing something potentially dangerous with stack allocated 
objects, and assigning a `scope` pointer to a not-scoped variable 
(`a.next` is not `scope` since this attribute is not transitive) 
is clearly dangerous since `connect` doesn't know which between 
`a` and `b` terminates first.




More information about the Digitalmars-d-learn mailing list