Port 22 #6

514.jpg

Another chaotic swirl.

The next problem with the software port was with AppDomains. An AppDomain is an isolated environment to run a program, or a part of a program. Every program runs in an AppDomain. The main program AppDomain is created when the program starts and taken down when the program exits. You never notice it or think about it. The tricky part is when you want more than one AppDomain.

A second AppDomain is useful with add-on code as described in the last couple of posts. If you load a program add-on into the main AppDomain, you cannot get rid of the add-on without terminating the main program. By loading the add-on into a separate AppDomain, you can discard the AppDomain, and the add-on code when you are done with it. I do this to recover the memory space used by the add-on, and to protect against crashes. The add-on code tends to be more experimental and less tested than the main program code.

My art program is the only program I have that uses more than one AppDomain. So I am deep into the port to VS 2022 when I learn that AppDomain support has been removed from .Net 6. Not changed, just flat out not supported.

The next best thing appears to be the AssemblyLoadContext class. You can load the add-on dll into a separate AssemblyLoadContext. When the add-on is done running the garbage collector eventually runs and frees the space. Well that is the theory at least.

There is a known bug with AssemblyLoadContext garbage collection when it runs WPF (Windows Presentation Foundation) code. WPF creates objects for windows and other display components. Some of these objects are never released, even after the window is closed. That prevents garbage collection. At the time I was discovering these problems the recommended work-around was the ever-so-helpful “just don’t do that”. In my case, my add-ons often come with custom editors which use WPF, so the recommended “solution” is not feasible. It may have been fixed by now, but I am not in a mood to fight that battle again.

I have given up on AppDomain and AssemblyLoadContext. Computers have so much memory these days, that worrying about add-on code memory use is a misplaced concerned. The memory used by the add-on program code is insignificant compared to the memory used for a single high resolution picture. As for program code defects, I just need to be more careful.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.