CompileAssemblyFromSource leaks handles

G

Guest

Microsoft.VisualBasic.VBCodeProvider.CompileAssemblyFromSource() leaks a
handle at every call. Under the IDE (VS2005), it leaks two handles at every
call. This behavior does not vary with different params (see sample below).

When GenerateInMemory is true and the compilation is successful, there is
also a memory 'leak' owing to the new assembly being added to the current
AppDomain and hence memory growth. This 'leak' has received comment in the
past, and it is not the topic of this inquiry. The handle leak I am talking
about is the process's handle count as reported by the task manager.

I have searched high and low for things to dispose/close/whatever. I have
found nothing beyond the dispose in the sample below. Am I missing
something? Or is this a CodeDom bug?

------------------------

Public Shared Function Problem() As String ' codedom leak problem
' CompileAssemblyFromSource() always leaks two handles as indicated by
task manager
' It also leaks about 1k memory if the compile was successful and
GenerateInMemory is true - an expected result

' make SourceCode, the code to compile
Dim aSourceCode() As String = { _
" Public Class DynamicClass
", _
" Public Shared Function DynamicFunction( Byval d as double ) as
boolean ", _
" Return d>0.5
", _
" End Function
", _
" End Class
"}
Dim SourceCode As String = Join(aSourceCode, vbLf)

' make params, the compiler parameters
Dim params As New CodeDom.Compiler.CompilerParameters
'params.GenerateInMemory = True ' Assembly is created in memory
params.OutputAssembly = "DynamicDll" ' or assembly created on a dll
'params.TreatWarningsAsErrors = True
'params.WarningLevel = 4
'params.CompilerOptions = "/optionexplicit+ /optionstrict+
/optioncompare:text /optimize- /debug+ /warnaserror+ /nowarn:42024"
'Dim refs() As String = {"System.dll", "Microsoft.VisualBasic.dll"}
'params.ReferencedAssemblies.AddRange(refs)

' make results by compiling the source code
Dim provider As New Microsoft.VisualBasic.VBCodeProvider
Dim results As CodeDom.Compiler.CompilerResults
results = provider.CompileAssemblyFromSource(params, SourceCode) ' cause
of the leak
provider.Dispose()

' make and return errors
' none of this is necessary for exhibiting the leaks
Dim errors As String = ""
If results Is Nothing Then
errors = "results is nothing"
Else
For Each out As String In results.Output
errors &= out & vbLf
Next out
End If
Return errors

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top