Thread overview
How to "dispose of" a string when using @live analysis?
Aug 29
Kagamin
Aug 30
Dukc
6 days ago
IchorDev
Aug 29
Dukc
Aug 29
Dukc
August 29

The following code fails to compile when using OB:

@live void show(scope ref string s) {
  import std.stdio;
  writeln(s);
}

@live void main()
{
    auto s = "foo bar";
    show(s);
    show(s);
}

Compile with:

dmd -preview=dip1021 ...

Error:

source/app.d(8): Error: variable `app.main.s` is not disposed of before return
    auto s = "foo bar";
         ^

How do I dispose of the string?

August 29

Docs suggest scope

scope s = "foo bar";
August 29

On Friday, 29 August 2025 at 17:13:21 UTC, Renato Athaydes wrote:

>

How do I dispose of the string?

static void* hackPtr;

// Use whenever you want to artificially end lifetime of a live variable
// Note that this can't be @live
void leak(T)(T x)
{   // Otherwise the compiler will infer `scope` for x,
    // defeating the purpose.
    if(false) hackPtr = &x;
}

@live void show(scope ref string s) {
  import std.stdio;
  writeln(s);
}

@live void main()
{
    auto s = "foo bar";
    show(s);
    show(s);
    //Since it's GC-allocated, "leaking" is how we "free" it in any case.
    leak(s);
}

Now, of course the problem is that you could use this leak to also "free" a malloced pointer, in which case it would literally be a memory leak. But at least you're doing it explicitly so you hopefully have a bit better change to spot yourself doing it than without @live.

Note that if you do all your allocation with the GC, there's no point in using @live. Well, many, me included, are of the opinion it's not worth it even if you don't but I'm still happy to see you test-driving the feature - it's always possible I'm wrong!

August 29

On Friday, 29 August 2025 at 20:50:00 UTC, Dukc wrote:

>

[snip]

Well duh, of course a string literal isn't GC-allocated as I claimed, but "allocated" by pointing to an immutable spot in preallocated static memory at string initialisation. Regardless, "leaking" works.

August 30

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, and if a variable is not scope, then it must be disposed of somehow when using @live!?

That bit of information was not clear to me from the docs.

Thanks Dukc for the leak hint, even if I don't use that (as you said, I don't need that for GC-allocated memory or "static" literals) it's illuminating to understand how that can be done.

August 30

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.

6 days ago

On Saturday, 30 August 2025 at 21:00:11 UTC, Dukc wrote:

>

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.

It really should not require this for memory that is GC-allocated. How would you even free immutable data without a garbage collector? You'd have to be sure nothing references it, otherwise it's not-so-immutable.