2

这是崩溃日志(短):

2013-04-24 19:56:24 +0000 Memotion 未处理的托管异常:对象引用未设置为
Pipedream.UI.UIElement.set_Size(Vector2 值)[0x00000] 中的对象实例(System.NullReferenceException):0
在 Pipedream.UI.StageLayer..ctor (Pipedream.ComponentID id) [0x00000] 在 :0
在 Pipedream.UI.Stage.CreateLayer (Pipedream.ComponentID id) [0x00000] 在 :0

实际上构造函数的代码是这样的:

internal StageLayer(ComponentID id)
    : base(id, ComponentLayer.System)
{
    base.Size = ApplicationBase.Instance.ScreenSize;
    ApplicationBase.Instance.PropertyChanged += HandlePropertyChanged;
}

该属性如下所示:

public virtual Vector2 Size
{
    get
    {
        return _Size;
    }
    set
    {
        Vector2 old = _Size;
        if (SetData(SizeDeclaration, value, ref _Size, Invalidate))
        {
            CompareUpdate(WidthDeclaration, _Size.X, old.X);
            CompareUpdate(HeightDeclaration, _Size.Y, old.Y);
        }
    }
}

好吧,这Vector2是一个结构,不能是null。此代码也可以在桌面上完美运行。我想不出这段代码应该崩溃的任何原因,但它也不会在 iOS 模拟器上,只在 iOS 设备上(我目前没有设备,所以我无法直接调试它)。

我在调用此代码之前启动了一些任务,但它们使用自己的数据并且不能与当前数据冲突,即使那样也不应该有任何NullReferenceException.

编辑

事实证明,CompareUpdate应该调用 -method 时会引发异常。无论如何,我认为非虚拟泛型方法不应该产生任何问题?

protected Boolean CompareUpdate<T>(DependencyProperty property, T newValue, T oldValue)
{
    if (!Object.Equals(newValue, oldValue))
    {
        ForceUpdate(property, newValue, oldValue);
        return true;
    }
    return false;
}

编辑 2

在更多的测试用例之后,我发现这可能是一个真正的编译器问题。以下测试失败:

Log.Info(_Size.X.ToString()); // _Size is still a struct

除了这个例外:

System.NullReferenceException:对象引用未设置为
System.Single.ToString () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Single.cs:260
at Pipedream的对象实例.UI.UIElement.set_Size (Vector2 值) [0x00031] 在 C:\WORK\00_PROJECTS\16 Pipedream\00_FRAMEWORK\trunk\Pipedream\UI\UIElement.cs:332

如果我将原始代码更改为以下也没有错误:

Vector2 old = _Size;
if (SetData(SizeDeclaration, value, ref _Size, Invalidate))
{
    CompareUpdate(WidthDeclaration, 0f, 0f);
    CompareUpdate(HeightDeclaration, 0f, 0f);
}

当我删除SetData-method 时也会发生此错误,因此这不是错误原因。我检查了 this 引用,所以堆栈似乎没问题,但是如果我尝试访问_Sizes 变量X并尝试将其打印到控制台,则 NullReferenceException 再次发生。

4

1 回答 1

0

事实证明,这有一个架构差异错误。行[StructLayout(LayoutKind.Sequential, Pack = 1)]导致Vector2Vector2 未对齐,这在某些体系结构中是非法的。

暂时删除该行可以解决此问题。

于 2013-06-01T21:33:55.623 回答