TheSwamp

Code Red => .NET => Topic started by: Peter Jamtgaard on June 11, 2010, 09:29:00 AM

Title: Disposal of a MDIActiveDocument Variable
Post by: Peter Jamtgaard on June 11, 2010, 09:29:00 AM
Swamp,

I was trying to create a simple print class function (or sub)

Code: [Select]
  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.

Code: [Select]
  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

Title: Re: Disposal of a MDIActiveDocument Variable
Post by: Glenn R on June 11, 2010, 09:37:50 AM
Code: [Select]
  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.
Title: Re: Disposal of a MDIActiveDocument Variable
Post by: Peter Jamtgaard on June 11, 2010, 10:02:13 AM
Hey Thanks a lot Glenn

I like the way you put that.

Quote
"The general rule is, if you 'new' it, delete it; if you didn't, don't"

 :-D

Peter
Title: Re: Disposal of a MDIActiveDocument Variable
Post by: Peter Jamtgaard on June 11, 2010, 10:16:34 AM
Here is another variation on that

If I create a function as TypedValue

Code: [Select]
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
Title: Re: Disposal of a MDIActiveDocument Variable
Post by: Glenn R on June 11, 2010, 10:24:43 AM
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.
Title: Re: Disposal of a MDIActiveDocument Variable
Post by: gile on June 11, 2010, 11:13:46 AM
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)