Confused about disposal of objects

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
...object created with New

...program runs

...users changes settings

...object is disposed and set to nothing

...object is created with New again

...program errors later down the line object not set?

Question:

So if I dispose an object to clean up the create it again using the New keyword will this fail as I disposed the object?
 
No, because you are creating a new instance of the object and
then setting it to the variable. You are not modifying the current
object in the variable; you are merely replacing it with another
object.
 
This is as I suspected, but consider this..

I have a render loop, when the loop ends I dispose the object. Then when the render loop needs to start again I create a new object which does not error. But when I try to load the mesh file I get an error saying the object is not set?

If I remover the dispose code I dont get an eror and all works OK??
 
Sure:)

this is the rendering part:

Code:
 stay in render loop until flag turns false

Do While m_blnRenderScene

    call render code

    Render()

    allow application time to do its stuff

    Application.DoEvents()

Loop

 dump current 3D object

If Not m_obj3DDevice Is Nothing Then

   m_obj3DDevice.Dispose()
   m_obj3DDevice = Nothing

End If

Once the code above has run prior to the render loop running again this code runs and returns true?

Code:
Public Function Init3D(ByVal D3DDeviceType As DeviceType) As Boolean

         attempt to setup a 3D device

         save the adapter number

        Dim intAdapter As Integer = Manager.Adapters.Default.Adapter

         get the current display format

        Dim dspDispMode As DisplayMode = Manager.Adapters(intAdapter).CurrentDisplayMode

         create a new presentation parameter object

        m_objPresentationParameters = New PresentParameters

         setup the presentation parameters

        m_objPresentationParameters.Windowed = True
        m_objPresentationParameters.SwapEffect = SwapEffect.Discard
        m_objPresentationParameters.BackBufferFormat = dspDispMode.Format
        m_objPresentationParameters.AutoDepthStencilFormat = DepthFormat.D16
        m_objPresentationParameters.EnableAutoDepthStencil = True

         try to create a 3D device

        Try

            m_obj3DDevice = New Device(Me.cboAdapter.SelectedIndex, D3DDeviceType, Me.picViewPort.Handle, CreateFlags.SoftwareVertexProcessing, m_objPresentationParameters)

             success

            Return True

        Catch objException As Exception

             failure

            Return False

        Finally

             tidy up

            dspDispMode = Nothing

            m_objPresentationParameters = Nothing

        End Try

End Function

The error occurs when I try to then load in the mesh file selected by user:

Code:
m_mshMesh = Mesh.FromFile(m_strMeshFile, MeshFlags.SystemMemory, m_obj3DDevice, extMaterials)

The error says object not set to an instance of...

If I remove the dispose bit of code above everything works fine. I cant understand why this should be happening??
 
Back
Top