Why would this (System.Drawing?) cause crash to desktop?

G

Guest

A user is reporting a crash to desktop to me, and I have isolated it to this
instantiation:

PanelColorsArray ourColors = new PanelColorsArray();

of this class:

// PanelColors.cs

using System;
using System.Drawing;

namespace GaugeGlow.Helpers
{
public class PanelColor
{
public PanelColor()
{
r = 0; g = 0; b = 0;
}

public PanelColor(int red, int green, int blue)
{
r = red;
g = green;
b = blue;
}

public int R
{
get { return r; }
set { r = value; }
}

public int G
{
get { return g; }
set { g = value; }
}

public int B
{
get { return b; }
set { b = value; }
}

private int r, g, b;
}

public class PanelColorsArray
{
public PanelColor[] panelColors; // Declare array

public PanelColorsArray()
{
panelColors = new PanelColor[] { cDay, cNight, cLuminous, cTemp };
panelColors.Initialize();
}

public void PanelColorsInit()
{
cDay.R = 255; cDay.G = 255; cDay.B = 255;
cNight.R = 10; cNight.G = 10; cNight.B = 10;
cLuminous.R = 40; cLuminous.G = 220; cLuminous.B = 20;
cTemp.R = 0; cTemp.G = 0; cTemp.B = 0;
}

PanelColor cDay = new PanelColor(255, 255, 255);
PanelColor cNight = new PanelColor(10, 10, 10);
PanelColor cLuminous = new PanelColor(40, 220, 20);
PanelColor cTemp = new PanelColor();
} // class: PanelColorsArray
} // namespace

The users must be on a min of XP Home SP2, which should have GDI+ installed,
so I'm clueless as to why this would result in a crash with the following
typical:

Event Type: Error
Event Source: .NET Runtime 2.0 Error Reporting
Event Category: None
Event ID: 5000
Date: 10/08/2006
Time: 7:49:05 PM
User: N/A
Computer: KYLE
Description:
EventType clr20r3, P1 gaugeglow.exe, P2 1.2.0.0, P3 44d863c7, P4
system.drawing, P5 2.0.0.0, P6 4333aeaf, P7 178, P8 26, P9
system.argumentexception, P10 NIL.

Thanks for any ideas. Patrick
 
M

Marc Gravell

The only oddity I can see is the call to Initialize() on the array; what do
you expect this to do?

Out of interest, what have you got against the Color struct? Could have
saved a lot of typing...

Marc
 
G

Guest

Marc,

Marc Gravell said:
The only oddity I can see is the call to Initialize() on the array; what do
you expect this to do?

Ah, an error! I deleted that. Was supposed to call PanelColorsInit() and
init the colors, but that was before I just initialized them below. So, I
deleted that and will try that and see if this stops the errors.
Out of interest, what have you got against the Color struct? Could have
saved a lot of typing...

I have a need in my program to set the RGB individually, and store it back
into the array.

ourColors.panelColors[curColor].R = (int)trackBarRed.Value;

If I use the Color structure, I can't set the colors individually, I have to
construct the whole Color each time. So, it was a performance decision.
 
N

Nicholas Paldino [.NET/C# MVP]

Stick,

This is what I would call a premature optimization. If you do some
profiling, I imagine that you will find that there is little performance
difference for using the Color structure vs your type.

On top of that, your color type is not compatable with the other API's
that expect a Color now, and my assumption is that you would have to do a
conversion, which is going to impact the performance anyways.

And while you would have to write some code to create a new Color
structure, it would be less code to maintain than the class you created.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Stick said:
Marc,

Marc Gravell said:
The only oddity I can see is the call to Initialize() on the array; what
do
you expect this to do?

Ah, an error! I deleted that. Was supposed to call PanelColorsInit() and
init the colors, but that was before I just initialized them below. So, I
deleted that and will try that and see if this stops the errors.
Out of interest, what have you got against the Color struct? Could have
saved a lot of typing...

I have a need in my program to set the RGB individually, and store it back
into the array.

ourColors.panelColors[curColor].R = (int)trackBarRed.Value;

If I use the Color structure, I can't set the colors individually, I have
to
construct the whole Color each time. So, it was a performance decision.
 
M

Marc Gravell

To quote Phil Davis: "Well, it's not good, but it's a reason..." ;-p

Best of luck getting it all working,

Marc
 
G

Guest

It really is not about optimization, but rather about having a class that
works the way I want to work. The Color structure is simply overkill for
what I need to do which is just store the 3 RGB values. As that structure
limited the way I could program, I rolled my own. It's not designed to be
used in API's.

Nicholas Paldino said:
Stick,

This is what I would call a premature optimization. If you do some
profiling, I imagine that you will find that there is little performance
difference for using the Color structure vs your type.

On top of that, your color type is not compatable with the other API's
that expect a Color now, and my assumption is that you would have to do a
conversion, which is going to impact the performance anyways.

And while you would have to write some code to create a new Color
structure, it would be less code to maintain than the class you created.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Stick said:
Marc,

Marc Gravell said:
The only oddity I can see is the call to Initialize() on the array; what
do
you expect this to do?

Ah, an error! I deleted that. Was supposed to call PanelColorsInit() and
init the colors, but that was before I just initialized them below. So, I
deleted that and will try that and see if this stops the errors.
Out of interest, what have you got against the Color struct? Could have
saved a lot of typing...

I have a need in my program to set the RGB individually, and store it back
into the array.

ourColors.panelColors[curColor].R = (int)trackBarRed.Value;

If I use the Color structure, I can't set the colors individually, I have
to
construct the whole Color each time. So, it was a performance decision.
 
M

Marc Gravell

and will try that and see if this stops the errors

If it doesn't, I'd add a top-level "catch", and dump the stacktrace
somewhere handy... it might be somewhere unrelated to this code. The
outermost exception /appears/ to be an ArgumentException, and your posted
code neither raises that nor does much (else) that might throw such an
error...

Marc
 
K

Kevin Spencer

I'm wondering why you call:
panelColors = new PanelColor[] { cDay, cNight, cLuminous, cTemp };

followed by:
panelColors.Initialize();

Effectively, this is intializing each member in the array using the default
constructor for its type. That would set all the values (r, g, b) of each
array member to 0.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Accept the Unexpected.

Stick said:
A user is reporting a crash to desktop to me, and I have isolated it to
this
instantiation:

PanelColorsArray ourColors = new PanelColorsArray();

of this class:

// PanelColors.cs

using System;
using System.Drawing;

namespace GaugeGlow.Helpers
{
public class PanelColor
{
public PanelColor()
{
r = 0; g = 0; b = 0;
}

public PanelColor(int red, int green, int blue)
{
r = red;
g = green;
b = blue;
}

public int R
{
get { return r; }
set { r = value; }
}

public int G
{
get { return g; }
set { g = value; }
}

public int B
{
get { return b; }
set { b = value; }
}

private int r, g, b;
}

public class PanelColorsArray
{
public PanelColor[] panelColors; // Declare array

public PanelColorsArray()
{
panelColors = new PanelColor[] { cDay, cNight, cLuminous, cTemp };
panelColors.Initialize();
}

public void PanelColorsInit()
{
cDay.R = 255; cDay.G = 255; cDay.B = 255;
cNight.R = 10; cNight.G = 10; cNight.B = 10;
cLuminous.R = 40; cLuminous.G = 220; cLuminous.B = 20;
cTemp.R = 0; cTemp.G = 0; cTemp.B = 0;
}

PanelColor cDay = new PanelColor(255, 255, 255);
PanelColor cNight = new PanelColor(10, 10, 10);
PanelColor cLuminous = new PanelColor(40, 220, 20);
PanelColor cTemp = new PanelColor();
} // class: PanelColorsArray
} // namespace

The users must be on a min of XP Home SP2, which should have GDI+
installed,
so I'm clueless as to why this would result in a crash with the following
typical:

Event Type: Error
Event Source: .NET Runtime 2.0 Error Reporting
Event Category: None
Event ID: 5000
Date: 10/08/2006
Time: 7:49:05 PM
User: N/A
Computer: KYLE
Description:
EventType clr20r3, P1 gaugeglow.exe, P2 1.2.0.0, P3 44d863c7, P4
system.drawing, P5 2.0.0.0, P6 4333aeaf, P7 178, P8 26, P9
system.argumentexception, P10 NIL.

Thanks for any ideas. Patrick
 
M

Marc Gravell

According to MSDN this method is even only really /defined/ for value
types... perhaps hence the bug?

OK null is cluse to 0, but...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top