pointer escaping return scope bug?

j my.email at gmail.com
Sun Dec 18 21:49:22 UTC 2022


On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven 
wrote:
> Hi,
> The following seems like a bug to me (reduced code, FILE* 
> changed to int*):
> ```d
> @safe:
>
> struct LockedFile
> {
>     private int* fps;
>
>     auto fp() return scope => fps;
> }
>
> void main()
> {
>     int* p;
>     {
>         auto lf = LockedFile(new int);
>         p = lf.fp;
>     }
>     assert(p != null); // address escaped
> }
> ```
> There's no error with -dip1000.
> I'll file this unless I overlooked something.



The state of the memory can not be guaranteed by the compiler. 
That is the responsibility of the programmer. When a pointer gets 
over-written the memory it was pointing to remains owne by the 
program. It could be accesible by recovering the pointer. If you 
want to destroy the memory there are various ways of doing this 
depending on your performance needs. D has an optional garbage 
collector by default.


More information about the Digitalmars-d-learn mailing list