Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

Why do static variables in a C# class library survive across object instantiated from VBA?

I wrote a plugin in the form of a registered C# class library which has COM exposed methods.

Static objects in the class seem to survive between calls of Sub Main. Why is that? Can I prevent it and completely 'reload/restart' the library? (I did set the ctrPlugin object to Nothing)

From VBA, I instantiate classes and call methods from the C# library like this:

Option Explicit

Sub Main
  Dim ctrPlugin
    Set ctrPlugin = CreateObject("MyLibrary.MyExposedClassName")
    ctrPlugin.OpenWpfWindowMethod
    Set ctrPlugin = Nothing
  End If 
End Sub

The problem I'm having with it, is an occasional "Out of memory" error. Which is shown in the host application (an ERP system) after the c# method is completed. The error never shows while running a c# method, always after it completed. I never have these issues when I work with C# applications. Is there a difference between object lifetimes and garbage collecting processes between C# application and class library?

I already memory profiled my library in visual studio, and got rid of some memleaks. (One of which was caused by WPF bindings to objects which weren't DependencyObjects or inherited INotifyPropertyChanged)

question from:https://stackoverflow.com/questions/65941614/why-do-static-variables-in-a-c-sharp-class-library-survive-across-object-instant

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...