Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How do I find why an ArgumentException is occurring?

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

Additional information: Parameter is not valid.

This is happening at the end of this code.

public void WriteStatusMessage(string message)
{
    m_ToolStripStatusLabelUserMessage.BackColor = WriteDefaultBackColor;
    m_ToolStripStatusLabelUserMessage.ForeColor = WriteDefaultForeColor;
    m_ToolStripStatusLabelUserMessage.Text = CommonConstants.Space + message;
    Update();
}

If I add a breakpoint in the code, it gets by that error, but happens on a later update. As far as I know, the only graphics code we're doing involves drawing graphs (which would be consistent with how I'm getting this error by trying to open a file of historic watch variable values when then display as graphs), and only employing Brushes, Pens, and Fonts (part of my recent changes was going through and adding "using" to ensure they got disposed of because we were getting memory leaks).

Mainly, I'm hoping someone can point me in the right direction to start decoding this. Much to my dismay, it's one of those cases where a large number of changes were made and checked in and I could swear I'd checked it before checking in, particularly as this action relates to the problem I was solving, but it's hard to argue with facts and the fact is that it's failing right now.

Thank you for any help you can give.

Edit: I have managed to revert to an earlier version on one of my check-ins, so I've got a general idea as to where to look for problems. It is indeed one of the graphics libraries, specifically the one that plots out the graphs which is a modification of the library at http://www.codeproject.com/KB/miscctrl/GraphComponents.aspx. As per the advice below, I'll start by looking at the Paint procedures I modified.

Further Edit: I found it. OnPaint, much as predicted:

protected override void OnPaint(PaintEventArgs e)
{
    if (!Visible)
        return;

    Graphics graphics = e.Graphics;
    Draw(graphics);

    base.OnPaint(e);
}

was turned into

protected override void OnPaint(PaintEventArgs e)
{
    if (!Visible)
        return;

    using (Graphics graphics = e.Graphics)
    {
        Draw(graphics);
    }       
    base.OnPaint(e);
}

when I was aggressively removing references to Drawing objects that didn't have a Dispose. Reverting that function removed the crashes. Unfortunately, it looks like I'm back to tracking down the memory leak I was searching for when I made these changes...

like image 664
Sean Duggan Avatar asked Nov 15 '11 22:11

Sean Duggan


1 Answers

Use Break on exceptions in Visual Studio. Menu: Debug->Exceptions and click in "thrown" on the exception you wish to find.

Oh and dont forget to debugcompile and run with debugger ;)

like image 197
stefan Avatar answered Oct 05 '22 23:10

stefan