使用 Mathnet 矩阵类型测试 (m == nullptr) 时出现 NullReferenceException
我正在努力解决一个奇怪的 NullReferenceException 问题,当我尝试比较从 MathNet 库中的 Matrix 类型派生的类型的属性时,会发生该异常 为 nullptr。
我想编写一个带有 Transformation 类的 C++/CLI 类库,该类库派生自 MathNet::Numerics::LinearAlgebra::Matrix,它应该将 3D 空间中的位置表示为齐次坐标中的 4x4 矩阵。因为我希望能够相对于其他位置设置位置,所以我有一个属性 Transformation^parent
。 通过 if(parent == nullptr){ ... }
我想测试当前 Transformation 是否有父级,但我在 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.
我的 Transformation 类如下所示:
/// 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");
}
}
将实际上为 null 的任何 Matrix 类型变量与 nullptr 进行比较似乎会引发此异常。如果正确初始化,则不会出现异常,因此可以正常工作:
Matrix^ m = gcnew Matrix(4,4);
if(m == nullptr) // works fine
{
Console::WriteLine("");
}
当从不同的类派生 Transformation 时,ref class Transformation : A
而不是 ref class Transformation : Matrix
,一切也都很好。
现在事情变得非常奇怪。我想在 C# 应用程序中使用我的类库。在 Transformation t 上调用 t.DoSomething() 会引发 NullReferenceException。但是,如果我直接在 C# 应用程序中包含 null 测试,它会起作用:
Transformation t = new Transformation();
// t.DoSomething(); // Throws NullReferenceException
if (t.parent == null) // OK!
{
Console.WriteLine("parent is null");
}
在 C++/ClI 应用程序中再次执行相同操作会引发 NullReferenceException:
Transformation^ t = gcnew Transformation();
// t->DoSomething(); // Throws NullReferenceException
if(t->parent == nullptr) // Throws NullReferenceException
{
Console::WriteLine("parent is nullptr");
}
任何建议,这可能来自何处?我真的很困惑......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C# 中,至少 == 运算符有可能以引发空引用的方式实现。
您可以尝试调用 Object::ReferenceEquals(obj, null) 看看是否有效?
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?