keepalive 1.1.1

Tools to ensure pointers are kept on the stack.


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Keepalive functionality

This is a small function to require pointers are stored on the stack. It guarantees that the pointer should be stored on the stack for at least the current scope.

Usage is pretty simple:

auto c = keepAlive(new C); // c will be stored on the stack
functionTakingC(c); // auto-converts to a C

otherCode();
GC.collect(); // will not collect c

Alternatively, you can call keepAlive and ignore the result, which will ensure that the compiler will keep the object alive until that line at least.

auto c = new C;
functionTakingC(c);

otherCode();
GC.collect(); // will not collect c
c.keepAlive; // until at least after this line

When to use

This is intended as a way to ensure stack storage for a pointer. This ensures that the GC can find the pointer. In most cases, if you are using this pointer later, it will be availble to the GC without this mechanism. The only time you would need to use this is if you are NOT using it later, AND it's only stored elsewhere in a location that the GC cannot find. In this case, its lifetime is only guaranteed to the end of the scope in which you stored the keepAlive result.

Alternatively, you can just call keepAlive on the pointer at a later time, and ignore the result, and the pointer lifetime will last at least until this call.

In most cases, using a deterministic registration and deregistration of a pointer is preferred, instead of letting the GC clean this up.

References:

Authors:
  • Steven Schveighoffer
Dependencies:
none
Versions:
1.1.1 2021-Sep-24
1.1.0 2021-Sep-23
1.0.0 2021-Sep-22
~master 2021-Sep-24
Show all 4 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 2 downloads this month

  • 2 downloads total

Score:
0.6
Short URL:
keepalive.dub.pm