How to "dispose of" a string when using @live analysis?
Dukc
ajieskola at gmail.com
Sat Aug 30 21:00:11 UTC 2025
On Saturday, 30 August 2025 at 13:41:18 UTC, Renato Athaydes
wrote:
> On Friday, 29 August 2025 at 19:28:05 UTC, Kagamin wrote:
>> Docs suggest `scope`
>> ```
>> scope s = "foo bar";
>> ```
>
> Ah, `scope` is not inferred with `auto`,
Local variable `scope` inference works only on need, such as when
you assign a slice of a stack-allocated static array to the
variable. The compiler doesn't try adding `scope` just because it
can (unlike with function parameters when function attribute
inference is on).
> and if a variable is not `scope`, then it must be disposed of
> somehow when using `@live`!?
Yes. Exactly once. That's the whole idea.
`@live` is supposed to guard against accidental memory leaks and
double frees. Functions like `free` or `leak` take the parameter
as non-`scope`, making sure you call them once and only once per
variable you have allocated. Functions like `writeln` that aren't
supposed to free data on your behalf take the parameter as
`scope`, meaning you can call them as many times as you want but
can't use them to dispose your variables.
More information about the Digitalmars-d-learn
mailing list