Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dispose/release/"finalize" unmanaged resources when a shared value gets out of scope

I have a type that encapsulates a key to an external resource. Once the key is lost (all values that share it get out of scope), the resource should be released (implicitly) on the next garbage-collection, like memory does for regular values.

So I'm looking for something similar to OOP disposing, or ForeignPtr, only that I represent references to something other than objects from foreign languages (although if ForeignPtr can properly and elegantly work for this too, knowing how would also answer this question).

Is it possible? if so, how?

like image 594
MasterMastic Avatar asked Jan 09 '23 10:01

MasterMastic


1 Answers

I suggest you look at ResourceT:

ResourceT is a monad transformer which creates a region of code where you can safely allocate resources.

like image 111
sinelaw Avatar answered Jan 25 '23 16:01

sinelaw