TheSwamp
Code Red => .NET => Topic started by: Peter Jamtgaard on June 11, 2010, 09:29:00 AM
-
Swamp,
I was trying to create a simple print class function (or sub)
Public Function Princ(ByVal strPrintString) As Boolean
'MsgBox(strPrintString)
Dim docThisDrawingP As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
docThisDrawingP.Editor.WriteMessage("" & strPrintString & vbLf & "")
docThisDrawingP.Dispose()
Return Nothing
End Function
When I call this function more than once it creates an exception.
The version below doesn't create an exception.
Public Function Princ(ByVal strPrintString) As Boolean
'MsgBox(strPrintString)
Dim docThisDrawingP As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
docThisDrawingP.Editor.WriteMessage("" & strPrintString & vbLf & "")
Return Nothing
End Function
Does the Dispose() method work globally?
I would assume (I know never assume anything) that the local docThisDrawing Document could be created and disposed numerous times in a run.
Any idea why it would cause that exception?
Would failure to dispose the document create a memory leak?
Am I missing something?
Peter
-
Public Function Princ(ByVal strPrintString) As Boolean
'MsgBox(strPrintString)
Dim docThisDrawingP As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
docThisDrawingP.Editor.WriteMessage("" & strPrintString & vbLf & "")
docThisDrawingP.Dispose()
Return Nothing
End Function
When I call this function more than once it creates an exception.
I'm not surprised. Never, ever, ever, dispose of a document object - AutoCAD is responsible for it. The general rule is, if you 'new' it, delete it; if you didn't, don't.
-
Hey Thanks a lot Glenn
I like the way you put that.
"The general rule is, if you 'new' it, delete it; if you didn't, don't"
:-D
Peter
-
Here is another variation on that
If I create a function as TypedValue
Dim tpvReturn As TypedValue = New TypedValue(LispDataType.Nil, -1)
.
.
.
Return tpvReturn
I want to return that value that I created with New, but I can't dispose of it after I return it.
Peter
-
If you really wanted to, the calling code would dispose of it - not the function code. Most times, the garbage collector does all the clean-up needed.
-
Peter,
You can read this (http://through-the-interface.typepad.com/through_the_interface/2008/06/cleaning-up-aft.html) and/or look at this (http://through-the-interface.typepad.com/.m/through_the_interface/2010/03/the-stephen-and-fenton-show-adn-devcast-episode-2.html)