On Tuesday, 24 April 2018 at 23:49:14 UTC, Steven Schveighoffer wrote:
What you are missing is that Database is pass-by-value, not a class. So when you include it directly in a class like you did in A, then when A's destructor is called, db's destructor is called.

Since in the first case, a is being destroyed by the GC, you get the error.

Ok, this makes sense.

In the second case (b), you aren't including the db by value, so no destructor is called from the GC. But this is dangerous, because db stops existing after main exits, but b continues to exist in the GC, so this is a dangling pointer.

If I set the pointer to null, before (b) is collected, would that work?

In the third case, scoped specifically destroys c when main exits, and you are not in the GC at that point.

What the error message is telling you is you should manually clean up the database directly instead of leaving it to the GC. What is the correct path? probably the scoped!A version. Though I'm not sure what making copies of the database does in that library.

-Steve

Ok I'll need to read the docs on scoped I think, but I think I understand.

If I wanted db to be persistent, but have temporary objects reference db without triggering GC collection of the db, you would use scoped? Or is this a situation where it's better to pass the db in function calls to objects rather than set as a member of these objects?

Reply via email to