Controls in panel causing memory leak?

mandelbrot

Centurion
Joined
Jul 1, 2005
Messages
194
Location
UK North East
Hi All,


I'm having a rather annoying problem at the moment with a form that is made up of a panel filled with labels that basically make up a three month calendar. When I change the visible property of the controls I seem to be suffering some kind of memory leak.

I've already had memory leaks previously (which I managed to fix), but this one seems to be beyond me...
Visual Basic:
    Private Sub RefreshCalendar(ByVal paramDate As Date)
        'Standard definitions...
        Dim dayCtl As Control
        Dim loopCounter As Integer
        Dim innerLoop As Integer
        Dim tempDate As Date
        Dim dayNum As Integer
        Dim calNum As Integer
        Dim levelPointer As Integer = 1
        'This section of code defines a reader to read in current dates, allowing the day display code to update the background
        'First, label up and tag the key controls...
        For loopCounter = 0 To 2
            'Work out the dates we're going to be playing with...
            tempDate = paramDate.AddMonths(loopCounter - 1)
            tempDate = New Date(tempDate.Year(), tempDate.Month(), 1)
            'Fill in the blanks for the years, months and days...
            For innerLoop = mcYear To mcDay
                monthCtl(mcYear, loopCounter).Tag = tempDate
                monthCtl(mcYear, loopCounter).Text = tempDate.Year()
            Next innerLoop
        Next loopCounter
        'Display the year separators...
        yearSep1.Visible = monthCtl(mcYear, 0).Text <> monthCtl(mcYear, 1).Text
        yearSep2.Visible = monthCtl(mcYear, 1).Text <> monthCtl(mcYear, 2).Text
        'We need to loop through all the controls in the form and figure out which calendar group each control is in.  From there we can
        'assign each day number based on the calendar group's month and year.  The grouping is also significant for days that should be 
        'hidden...
        For Each dayCtl In datePanel.Controls
            If dayCtl.Name.StartsWith("day") Then
                'Get the number substring from the control and convert it to an integer...
                dayNum = Convert.ToInt16(dayCtl.Name.Substring(3))
                calNum = Int((dayNum) / 42)
                'Populate the tag and text based on the values stored in the month control at the top of the block...
                dayCtl.Tag = DateAdd(DateInterval.Day, dayNum - (calNum * 42), monthCtl(mcDay, calNum).Tag)
                dayCtl.Text = dayCtl.Tag.Day().ToString()
                'The control should only be visible if it's within the bounds of the current month...
                [color=red]dayCtl.Visible = dayCtl.Tag.Month().Equals(monthCtl(mcMonth, calNum).Tag.month())[/color]
                If dayCtl.Tag.Equals(Now().Date()) Then 'Format the day boxes...
                    dayCtl.ForeColor = todayColor
                Else
                    dayCtl.ForeColor = Color.Black
                End If
                If Not dayCtl.BackColor.Equals(Color.FromKnownColor(KnownColor.Control)) AndAlso _
                        dayCtl.Tag.DayOfWeek = 0 Or dayCtl.Tag.DayOfWeek = 6 Then
                    dayCtl.BackColor = Color.FromKnownColor(KnownColor.ControlLightLight)
                Else
                    dayCtl.BackColor = Color.FromKnownColor(KnownColor.Control)
                End If
            End If
        Next dayCtl
    End Sub
The program falls out at the line indicated. Can anyone spot anything directly obvious that may affect memory?

monthCtl is simply an array that contains a set of references to objects on the form (labels). The yearSeps are two picture boxes that are turned on and off between years.

Thanks in advance,
Paul.
 
Does it fail with a particular error and is it the same error every time? What makes you think it is a memory related issue? i.e. The error message or are you running a profiling tool and tracking a memory leak?
 
LOL! Hi PD - I think you should be included on my Christmas Card list this year! ;) You seem to answer all of my problems!

Well, the only message that is displayed is the SystemOutOfMemory exception. I've looked around the internet for any pointers as to what might be causing this, but haven't found anything.

Originally, I had code in which searched a database and coloured the backgrounds of the labels. This did cause a memory leak, as I forgot to properly instantiate a collection that handled parameters for the SQL query, but I solved this and the whole thing actually worked. The only problem was that no test data was returned...!

I removed the SQL label formatting section to test the form again with someother formats, and before I could even set the formats I got another memory leak.

The panel that holds the three calendars is made up of 132 labels, two picture boxes and two buttons.

The only reason (bar the error message) that I think it's a memory leak is that there's nothing else to go wrong! Surely VS.NET can deal with less than 150 (total) controls on the form?


Paul.
 
You might want to try adding
Visual Basic:
Option Strict On
to the top of the module and fix any errors it generates first as you seem to be doing a lot of implicit casting in regards to the date handling with the tag property. Also how in the monthCtl array defined?
If you run a tool like TaskManager or the ProcessExplorer from www.sysinternals.com while executing your code does the available memory drop noticably?
 
Hmm... I didn't think of Opion Strict. I'll give that a go, see what it produces.

I haven't had chance to check memory drops, though - I'll give that a go when I get to work...
 
I added the Option Strict in, as you suggested, and modified all of the suspect code that was highlighted (obviously all dubious conversions that needed making), and the form now runs. Unfortunately, I'm going to have to go back through it all now, and check to see why the controls no longer display the correct settings, ... but I did expect something like that to happen anyway!

Thanks for all of your help PD - it's greatly appreciated.


Paul.
 
Back
Top