| Not when the language also supports value types and region allocators (e.g. IDispose in .NET). You can even turn it into RAII proper, by turning into a compilation error not handling those interfaces properly. Again with .NET, there are SafeHandles as well, alongside the MarshalInterop APIs. This is nothing new actually, Mesa/Cedar for Xerox PARC used reference counting with a cycle collector, while other descendent languages down to Modula-3 and Active Oberon always combined value types, tracing GC and C++ like resource handling capabilities. Oh Common Lisp also has similar capabilities, specially the ZetaLisp predecessor from Lisp Machines. Then Eiffel not only had this, it was also probably the first Algol like language to support non nullable references. Sadly they decided to ignore all of this in Java, and then its world domination kind of made everyone else ignore it as well. Thankfully even Java is improving their story in this regard, while languages like D, Nim and yes .NET kind of show what was already available for several decades. |
Seems to me it is impossible to have both automatic precise release of a resources and collection-based GC?
As I understand it, even the documentation for IDisposable in .NET says as much at https://docs.microsoft.com/en-us/dotnet/api/system.idisposab...:
> The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.
> Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.
So this is the interface you can use to explicitly release a resource, because the GC gets around to it only later at some unspecified time.
About SafeHandle it says at https://docs.microsoft.com/en-us/dotnet/api/system.runtime.i...:
> The SafeHandle class provides critical finalization of handle resources, preventing handles from being reclaimed prematurely by garbage collection and from being recycled by Windows to reference unintended unmanaged objects.
Doesn't seem it's at all helpful for automatic precise release of resources.