Here's the code fragment of the function. :
private void BlastOff(Mass mass, float dt)
{
Unit unit = mass.Unit;
if (null == unit || ! unit.Enabled)
return;
The stack trace (which I can't seem to find right now) definitely
nails the 'if' statement as the culprit.
Well, what is "unit" in this case? If it overloads the == operator,
that could be the problem. Are you *absolutely* sure it's actually
occurring on this line, rather than near it? Could you post some more
code?
Unit does override the CompareTo() function. By the variable naming
convention of the function, I most likely stole this right out of the
C# docs:
public enum UnitType
{
Apple,
Orange,
Elephant,
}
[XmlAttribute("type")]
public UnitType Type
{
get{return unitType;}
set{unitType=value;}
}
public int CompareTo(object obj)
{
Unit u = (Unit)obj;
if(this.Type == u.Type)
return 0;
else if (this.Type > u.Type)
return 1;
else
return -1;
}
I could see how this may generate a null reference exception, but like
I said, I get the null reference exception randomly. And the unit
object is null pretty much most of the time.
I will note that I'm using the DirectX API. It is using native
interop calls, possibly backing up my memory corruption claim.
While we're at it, I have another null reference exception that makes
no sense. Granted, the DX API may be having some issues of its own.
The setup for this is that I'm accessing an object out of an array
several times. (I own none of the implemenation behind this code, it's
all DirectX calls). 99.99% of the time, no problems. Once, I got this
null reference exception:
Code Frag:
device.TextureState[0].AlphaOperation = TextureOperation.Modulate;
device.TextureState[0].AlphaArgument0 = TextureArgument.Current;
device.TextureState[0].AlphaArgument1 =
TextureArgument.TextureColor;
device.TextureState[0].AlphaArgument2 = TextureArgument.Diffuse;
device.TextureState[0].ColorArgument0 = TextureArgument.Current; //
null reference happens here
device.TextureState[0].ColorArgument1
= TextureArgument.TextureColor;
device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
device.TextureState[0].ColorOperation = TextureOperation.Modulate;
Here's the stack trace.
System.NullReferenceException: Object reference not set to an instance
of an object.
at Microsoft.DirectX.Direct3D.TextureStates.get_TextureState(Int32
index)
at Lib.Rasterizer.MonkeyBusiness.SpriteBegin() in C:\Documents and
Settings\bladieblabla\My
Documents\Project\Rasterizer\MonkeyBusiness.cs:line 368
at Lib.Rasterizer.MonkeyBusiness.Draw(Vector2 destination, Int32
frameNumber, Int32 alpha) in C:\Documents and Settings\bladieblabla\My
Documents\Project\Rasterizer\MonkeyBusiness.cs:line 394
One thing I notice is that I get these in clusters. When I reboot,
they seem to go away for ahile.
My major problem is that I get these, and I can't find the root cause.
In C++, I would be looking for that stray pointer. C#, what could
have a stray pointer?