Prevent Garbage Collector

anonymous anonymous at example.com
Sat Jan 4 10:01:00 PST 2014


On Saturday, 4 January 2014 at 17:15:12 UTC, Jeroen Bollen wrote:
> Is there a way to prevent the Garbage collector from running on 
> one particular object? Something like:
>
> int* CreatePermanentInt() {
>     int i = 5;
>     return &i;
> }
>
> And i wouldn't be collected after this.

i isn't GC managed here. It's a stack variable. Returning
references to the stack is a no-no, because the stack will be
reused.

If i was actually GC managed, it would not be collected, because
there's a reference to it:
----
int* CreatePermanentInt() {
      int* i = new int;
      *i = 5;
      return i;
}
----


More information about the Digitalmars-d-learn mailing list