NullReferenceException when testing (m == nullptr) with Mathnet Matrix type

1.1k Views Asked by At

I am struggeling with a weird NullReferenceException which occurs when I try to compare an attribute of a type derived from a Matrix type from the MathNet library to nullptr.

I want to write a a C++/CLI Class Library with a class Transformation, derived from MathNet::Numerics::LinearAlgebra::Matrix, which should represent Positions in 3D Space as a 4x4 Matrix in homogeneous coordinates. Because I want to be able to set positions relative to other positions i have an attribute Transformation^ parent. By if(parent == nullptr){ ... } I want to test, if the current Transformation has a parent, but I get this Exception in the line with if(parent == nullptr):

An unhandled exception of type 'System.NullReferenceException' occurred in MathNet.Iridium.dll 
Additional information: Object reference not set to an instance of an object.

My Transformation class looks like this:

/// Transformation.h
using namespace MathNet::Numerics::LinearAlgebra;
using namespace System;
ref class Transformation : Matrix
//ref class Transformation : A
{
public:
    Transformation(void);
    Transformation^ parent;
    void DoSomething();
};


/// Transformation.cpp
#include "StdAfx.h"
#include "Transformation.h"
Transformation::Transformation(void) : Matrix(4,4)
{
}
void Transformation::DoSomething()
{
    if(parent == nullptr)   // Produces NullReferenceException
    {
        Console::WriteLine("parent is nullptr");
    }
    Matrix^ m;
    if(m == nullptr)        // Produces NullReferenceException, too
    {
        Console::WriteLine("m is nullptr");
    }
}

Comparing any variable of Matrix type, that is actually null, to nullptr seems to throw this Exception. If it is initialized properly there is no Exception, so this works fine:

Matrix^ m = gcnew Matrix(4,4);
if(m == nullptr)        // works fine
{
    Console::WriteLine("");
}

When derive Transformation from a different class, ref class Transformation : A instead of ref class Transformation : Matrix, everything works fine, too.

And now it gets really weird. I wanted to use my class library in a C#-Application. Calling t.DoSomething() on a Transformation t throws the NullReferenceException. BUT, if I include the null-test directly in my C# Application, it works:

Transformation t = new Transformation();
// t.DoSomething();      // Throws NullReferenceException
if (t.parent == null)    // OK!
{
    Console.WriteLine("parent is null");
}

Doing the same in a C++/ClI application again throws the NullReferenceException:

Transformation^ t = gcnew Transformation();
// t->DoSomething();     // Throws NullReferenceException
if(t->parent == nullptr) // Throws NullReferenceException
{
    Console::WriteLine("parent is nullptr");
}

Any suggestions where this might come from? I am really quite puzzled...

I use the MathNet.Idirium Library, Version 2008.8.16.470

1

There are 1 best solutions below

6
On BEST ANSWER

Its possible in C# at least for the == operator to be implemented in a way that throws a null reference.

Can you try calling Object::ReferenceEquals(obj, null) and see if that works or not?