Author Topic: Disposal of a MDIActiveDocument Variable  (Read 2446 times)

0 Members and 1 Guest are viewing this topic.

Peter Jamtgaard

  • Guest
Disposal of a MDIActiveDocument Variable
« 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


Glenn R

  • Guest
Re: Disposal of a MDIActiveDocument Variable
« Reply #1 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.

Peter Jamtgaard

  • Guest
Re: Disposal of a MDIActiveDocument Variable
« Reply #2 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

Peter Jamtgaard

  • Guest
Re: Disposal of a MDIActiveDocument Variable
« Reply #3 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

Glenn R

  • Guest
Re: Disposal of a MDIActiveDocument Variable
« Reply #4 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.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Disposal of a MDIActiveDocument Variable
« Reply #5 on: June 11, 2010, 11:13:46 AM »
Peter,

You can read this and/or look at this
Speaking English as a French Frog