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

1

7

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 which 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...

Pierian answered 15/11, 2011 at 22:27 Comment(4)
Does it happen in the Update() method? If so, try debugging step by step in the method (Use F11).Tooling
It does... but it's the standard Windows.Forms Update function that you can't step into.Pierian
Oh sorry, I thought it was a method of yours.Tooling
It was indeed in an OnPaint. Good call!Pierian
J
9

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 ;)

Jacksonjacksonville answered 15/11, 2011 at 22:31 Comment(5)
And don't forget the add comment link.Guzel
@Henrik I've used it so much tonight i had to re-learn the answer button.Jacksonjacksonville
+1. You also may need to turn off "my code only" (tools->options->debugging) if exception is coming from .Net / some other code that VS does not consider "yours".Evenhanded
Unfortunately, that drops me in the same location as before, but thank you for the suggestion.Pierian
It's a setting I'd turned off a while ago while chasing another issue. I just checked again and it's still off. On the bright side, thanks to Subversion, I've managed to trace the issue down to one file. Once I got it down to two, I was pretty sure which one was the culprit. I was wrong. Still tracking down exactly what went wrong. :) It's progress.Pierian

© 2022 - 2024 — McMap. All rights reserved.