I have 2 classes, DataDiff
and SimpleDataDiff
. SimpleDataDiff
inherits DataDiff
. DataDiff
has two virtual Properties, SimpleDataDiff
overrides both of them. I'm using Visual Studio 2013.
They look like this:
class DataDiff
{
public virtual Data Left { get; private set; }
public virtual Data Right { get; private set; }
public DataDiff(Data left, Data right)
{
Left = left; // Debugger crash
Right = right;
}
}
class SimpleDataDiff : DataDiff
{
public override Data Left
{
get { return Left as SimpleData; }
}
public override Data Right
{
get { return Right as SimpleData; }
}
public SimpleDataDiff(SimpleData left, SimpleData right) :
base(left, right)
{
}
}
Data
and SimpleData
only hold some variables and SimpleData
inherits Data
.
Now if I debug a line like SimpleDataDiff diff = new SimpleDataDiff(left, right);
where left and right are instances of SimpleData
and step into the constructors until I reach the assignment of the Left
property in DataDiff
the debugger crashes.
The program 'program.exe' has exited with code -2147023895 (0x800703e9).
When I tried changing the property in Data
to public Data Left { get; private set; }
and in SimpleData
to public new SimpleData Left { get; }
it stopped crashing. That's why I thought it could have somehthing to do with the virtual keyword. Am I missing something obvious here?
Also, if I step over the constructor call, the debugger doesn't crash but if I try to open the DataDiff
object in the Locals tab of the debugger, it crashes. Can somebody explain to me why this happens?
public class Data
{
public string Shortname { get; set; }
public uint StartByte { get; set; }
public uint ByteLen { get; set; }
}
class SimpleData : Data
{
public bool IsHighLowByteOrder { get; set; }
public uint StartBit{ get; set; }
public uint BitLen { get; set; }
}
In your SimpleDataDiff
derived class, you are overriding the base class properties like so:
public override Data Left
{
get { return Left as SimpleData; }
}
This results is a circular resolution attempt, as the debugger tries to resolve the reference when you are stepping through the code. This should really be:
public override Data Left
{
get { return base.Left; } // the 'as SimpleData' is not necessary
}
Strangely enough, this does not generate a warning on Visual Studio 2015 (or, apparently, on VS 2013 either). You may want to consider filing an issue with Microsoft.
User contributions licensed under CC BY-SA 3.0