West Wind Internet and Client Tools
wwDotnetBridge - Run destroy code from .net class not occurring until exit VFP
Gravatar is a globally recognized avatar based on your email address. wwDotnetBridge - Run destroy code from .net class not occurring until exit VFP
  David Shrader
  All
  Jun 3, 2024 @ 07:25am

I am trying to get some code to run from a destroy/finalize event in my .net class. in VFP I tried the following

  • o = null --> Did not fire the .net destroy/finalize
  • Release o --> Did not fire the .net destroy/finalize
  • goDotNetBridge.Unload() --> Did not fire the .net destroy/finalize
  • goDotNetBridge = null --> Did not fire the .net destroy/finalize
  • release goDotNetBridge --> Did not fire the .net destroy/finalize

Finally, quitting VFP did fire the destroy method. I understand that this is tied to the .net runtime. I tried the goDotNetBridge.Unload() since it seems to be the thing to do.

*  Unload
****************************************
***  Function: Unloads the CLR and AppDomain
***    Assume: Don't call this unless you want to explicity force
***            the AppDomain to be unloaded and a new one to be spun
***            up. Generally a single domain shoudl be sufficient.
***      Pass:
***    Return:
************************************************************************
FUNCTION Unload()
IF VARTYPE(this.oDotNetBridge) == "O"
	this.oDotNetBridge = NULL	
	DECLARE Integer ClrUnload IN WWC_CLR_HOSTDLL	
	ClrUnload()
ENDIF

Am I missing something? Please help.

Gravatar is a globally recognized avatar based on your email address. re: wwDotnetBridge - Run destroy code from .net class not occurring until exit VFP
  Rick Strahl
  David Shrader
  Jun 3, 2024 @ 12:25pm

In short there's no way to explicitly trigger an object to unload. The only way is to:

  • ensure there are no other outstanding references
  • all resources the object uses internally are also released

You have to make sure that the object is cleaned up and there are no outstanding references. .NET typically doesn't use Finalizers - instead most objects that use resources uses an IDisposable interface and you can call .Dispose() to clean up object resources.

Finalizers are tricky and generally a bad idea in .NET because they can only be triggered by the Garbage collector. If there are outstanding references to the object or inside of the the object, it may never release... hence the deterministic IDisposable implementation that can be externally triggered (and is automatic for many things in .NET internally - like Dependency Injection, which automatically disposes objects when the instance expires).

+++ Rick ---

© 1996-2024