Could somebody please explain the following

Data Danger

Freshman
Joined
Mar 25, 2004
Messages
29
I'm trying to track down a memory leak/Unreleased object that seems to happen when I use a particular way of opening a form. I have not included all dispose references but they are there in my code.

I have created a small test program that has 3 forms. Form1 just opens Form2 and Form2 will open up my report viewer and display my report.

Form2 is opened this way in button_click
Visual Basic:
 Dim f2 As Form = New Form2
 f2.ShowDialog()
 f2.Dispose()
Form2 has a button to open up my viewer

If I use

Form2 -
Visual Basic:
Private Sub btnViewReport1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewReport1.Click

        Form4.ShowReport(New ddSummaryReport)
        Form4.ShowDialog()
        Form4.Dispose()

End Sub
Form4 has the ShowReport method

Then the memory of Form4 is not release correctly

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

Now if I use
Visual Basic:
Private Sub btnViewReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewReport.Click
        Me.ShowReport(New ddSummaryReport())
End Sub

    Private Sub ShowReport(ByVal rpt As ActiveReport)

            Try

            Me.connectString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet"
            rpt.DataSource = "SELECT What I Want"

            Dim _ds As OleDBDataSource = CType(rpt.DataSource, OleDBDataSource)

             ' Create a new instance of the viewer form, pass document and parent form:
          
  Dim _viewerForm As New Form3(rpt)
            _viewerForm.Show()

        Catch ex As ReportException
            MessageBox.Show(ex.ToString())
        End Try

    End Sub 

Public Class Form3
Public Sub New(ByVal rpt As ActiveReport)

        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        Me.Text = rpt.Document.Name
        viewerReport = rpt
        rpt.Run(True)
        arvMain.Document = rpt.Document

    End Sub
End Class
When I use the code above the Memory does get released correctly. Could somebody explain why this is? Also is it aways better to open an instance to another form by using the Sub New method.

-Paul
 
Last edited by a moderator:
Without seeing the actual code it is pretty difficult to check where an object may be failing to be released correctly - have you considered using try ... finally blocks around your allocated resources (or the Using statement if .Net 2).

Also how are you checking if the object has been released or not.
 
Hi Thank for replying

The objects that do not get release are from a third party piece of software so I do not have control of that. The object does have a dispose method so I use it.

I'm using .NET Memory profiler. This shows me the current level of memory in use. If I use the first method then 1.7mb does not get released when I use the second it all gets released.

As I mentioned if a use (this is without creating a instance) Form4.ShowReport(New Report), Form4.ShowDialog, Form4.Dispose then objects do not get released. The process to start the report viewer is run inside Form4. If I use a Private Sub New(ByVal rpt As ActiveReport) and create an instance that way then the objects do get released correctly. The code is the same, I have just used a different way of creating the instance. So I was only wondering why this would be. The disposal of objects are the same, one way release them the other does not.

If you would like me to email you the code then please ask.

-Paul

VS2005 VB.NET 2.0
 
Back
Top