为什么删除(DictionaryInstance [关键]);失败? [英] Why does delete( DictionaryInstance[ key ] ); fail?

查看:272
本文介绍了为什么删除(DictionaryInstance [关键]);失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用字典

protected _categoryToValueDict:Dictionary = new Dictionary();

一些映射到别的东西。

to map something to something else.

现在,在某一个点在应用程序中,我需要删除从词典某个键

Now, at a certain point in the application, I need to remove a certain key from the Dictionary.

我实现了这个简单的方法:

I implemented this simple method:

    public function setCategoryNoValue( cat:TAModelCategory ):void {

        // delete( _categoryToValueDict[ cat ] );

        var old:Dictionary = _categoryToValueDict;

        _categoryToValueDict = new Dictionary();

        for ( var key:* in old ) {

            if ( key != cat ) {
                _categoryToValueDict[ key ] = old[ key ];
            }
        }

    }

如果我只用[的<说明href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#delete">delete运营商]

If I only use [description of the delete operator]

delete( _categoryToValueDict[ cat ] );

该应用本身不会在正常模式下引发错误。但只要我的连载的外部数据结构的外部源[当前的共享对象],在应用程序是不能够反序列化它以后。

the app itself doesn't throw errors in normal mode. But as soon as I serialize its external data structure to an external source [currently SharedObject], the app isn't able to de-serialize it later on.

如果我用上面的codeD 手动迭代切除手术,在反序列化操作的运行正常和模型出现在应用程序。

If I use the above coded manual iterative removal operation, the de-serialize operation works as expected and the model appears in the app.

替代应该相同。难道他们不应该?

The alternative should be identical. Shouldn't they?

因此,我的问题:什么是两种选择之间的差异

PS:这个问题的也许的是与<一href="http://stackoverflow.com/questions/7681374/serialize-circular-object-networks-using-writeobject-readobject">my previous 之一。

PS: This question might be related to my previous one.

更新-1

的Adobe解释了此页面

如果要使myObject的符合垃圾收集引用的对象,则必须删除所有引用。在这种情况下,必须更改myObject的值并从myMap中删除myObject的关键,如下面的code:

To make the object referenced by myObject eligible for garbage collection, you must remove all references to it. In this case, you must change the value of myObject and delete the myObject key from myMap, as shown in the following code:

myObject = null;
delete myMap[myObject];


是假设这是一个错字。难道不应该这样写:


Is suppose this to be a typo. Shouldn't it read like this:

delete myMap[myObject];
myObject = null;

为什么要传递一个空指针MYMAP关键?

Why pass a null-pointer to myMap as key?

推荐答案

好吧,我只花了一个很好的两小时左右寻找到这一点,这是远远超过我打算花看着这一点。但是,我很感兴趣。

Okay, I just spent a good two hours or so looking into this, which is way more than I planning on spending looking at this. But I was intrigued.

我想你可能会发现在ActionScript的AMF编码合法错误(或如何使用词典类被通过AMF seralized)。该漏洞影响任何使用AMF,所以完全相同的错误是可重复执行与的ByteArray ,所以我要利用它来进行演示。

I think you may have uncovered a legitimate bug in ActionScript's AMF encoding (or in how the Dictionary class gets seralized via AMF). The bug effects anything that uses AMF, so the exact same bug is reproduceable with a ByteArray, so I'm going to use that for demonstration purposes.

考虑以下code:

        var d:Dictionary = new Dictionary(false);
        d["goodbye"] = "world";
        d["hello"] = "world";
        delete d["hello"]

        var ba:ByteArray = new ByteArray();
        ba.writeObject(d);

        var len:uint = ba.position; 
        ba.position = 0;
        for(var i:uint=0;i<len;i++) {
            trace(ba.readUnsignedByte().toString(16));
        }

输出将是:

11 05 00 06 0F 67 1207米1207米64 62 79 65 06 0B 77 6F 72 6C 64

现在,如果我们不把过什么你好作为一个关键的:

Now what if we don't ever put the "hello" in as a key:

        var d:Dictionary = new Dictionary(false);
        d["goodbye"] = "world";

        var ba:ByteArray = new ByteArray();
        ba.writeObject(d);

        var len:uint = ba.position; 
        ba.position = 0;
        for(var i:uint=0;i<len;i++) {
            trace(ba.readUnsignedByte().toString(16));
        }

输出则是:

11 03 00 06 0F 67 1207米1207米64 62 79 65 06 0B 77 6F 72 6C 64

注意,长度是完全一样的,但是它们在第二个字节不同。

Notice that the length is exactly the same, however they differ in the second byte.

现在让我们看一下序列化的,如果我不删除你好

Now lets look at the serialization for if I don't delete "hello":

11 05 01 06 0B 68 65 6C 6C 6F 06 0B 77 6F 6C 72 64 06 0F 67 1207米1207米64 62 79 65 06 02

注意 05 在第二个字节是一样的,当我们将其删除。我认为这是指定在字典中的项目数。我说:我想,因为我通过AMF0 / 3的文档挖了好一阵子试图弄清楚到底什么怎么回事,因为它似乎并不像这应该是序列化的词典,但其相当一致,但我不明白这一点。

Notice that 05 in the second byte is the same as when we deleted it. I think this is specifying the number of items in the Dictionary. I say "I think" because I dug through the documentation on AMF0/3 for quite a while trying to figure out exactly whats going on here, because it doesn't seem like this should be the serialization for a Dictionary, but its fairly consistent, but I don't get it.

所以,我想这就是为什么你打的异常(最终文件明确的错误),因为其仍认为应该在字典中的另一个项目,它应该是反序列化。

So I think that's why you are hitting an exception (specifically the "End of file" error), because its still thinks there should be another item in the dictionary that it should be de-serializing.

您替代方法的工作,因为你正在建设一个新的字典,并填充它...它的内部计数器只有不断提高,所以它的工作原理就像一个魅力。

Your alternate method works because you are constructing a new Dictionary and populating it... Its "internal counter" is only ever increasing, so it works like a charm.

另外要注意的是,如果你设置 D [你好] =未定义,它不会抛出异常,但该项目确实的不是的获得从字典中删除。关键被序列化与的值在AMF流未定义。所以生成的字节流长于如果这是从来没有在那里。

Another thing to note, that if you set d["Hello"] = undefined, it does not throw an exception, but the item does not get removed from the dictionary. The key gets serialized with a value of undefined in the AMF stream. So the resulting byte-stream is longer than if it was never there.

使用对象似乎并没有表现出同样的行为。不仅不会不产生错误,所生成的字节code是更符合AMF0 / 3文档,我能找到从Adobe线。而随之而来的钥匙是从字面上系列化下降,喜欢它实际上从来没有在那里。所以,我不知道有什么特殊的情况下,他们正在使用词典(显然是无证AMF3数据类型 0×11 ),但它不会删除的项目出来玩耍的权利。

Using an Object doesn't seem to exhibit this same behavior. Not only doesn't not produce an error, the generated bytecode is more in line with the AMF0/3 documentation I could find from Adobe. And the resulting "key" is literally dropped from the serialization, like it was in fact never there. So I'm not sure what special case they are using for Dictionary (apparently the undocumented AMF3 datatype 0x11), but it does not play right with deleting items out of it.

这似乎是一个合法的错误给我。

It seems like a legit bug to me.

修改

所以,我周围挖了一点,发现其他人谈论一个<$ C $的AMF serilization C>词典

So I dug around a bit more and found other people talking about AMF serilization of a Dictionary.

0x11 : Dictionary Data Type
0x05 : Bit code: XXXX XXXY
     : If y == 0 then X is a reference to a previously encoded object in the stream
     : If y == 1 then X is the number of key/val pairs in the dictionary.

因此​​,如果这种情况下 5和1 == 1 5 GT;&GT; 1 == 2 ,所以该公司预计,在坏的序列化版本两个关键/缬氨酸对。

So if this case 5&1 == 1 and 5>>1 == 2, so it's expecting two key/val pairs in the "bad" serialized version.

这篇关于为什么删除(DictionaryInstance [关键]);失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆