4
Vote

Empty Set<> serialization bug

description

This sample crashes then the Set is empty.
 
new BinaryFormatter().Serialize(new MemoryStream(), new Set<int>());

file attachments

comments

lolofresnes wrote Oct 25, 2007 at 2:15 PM

Replace the attached file to fix this issue.

wrote Oct 25, 2007 at 2:15 PM

wrote Nov 22, 2007 at 6:48 AM

wrote Dec 1, 2007 at 10:09 PM

wrote Dec 19, 2007 at 4:06 PM

MihaMarkic wrote Dec 23, 2007 at 7:43 PM

Here is the fixed code if anybody is interested:
    void IDeserializationCallback.OnDeserialization(object sender)
    {
        if (serializationInfo == null)
            return;

        loadFactor = serializationInfo.GetSingle("loadFactor");
        equalityComparer = (IEqualityComparer<T>) serializationInfo.GetValue("equalityComparer", typeof(IEqualityComparer<T>));

        T[] items = (T[])serializationInfo.GetValue("items", typeof(T[]));
        if (items != null)
        {
            T dummy;

            EnsureEnoughSlots(items.Length);
            foreach (T item in items)
                Insert(item, true, out dummy);
        }

        serializationInfo = null;
    }
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        if (info == null)
            throw new ArgumentNullException("info");

        info.AddValue("equalityComparer", equalityComparer, typeof(IEqualityComparer<T>));
        info.AddValue("loadFactor", loadFactor, typeof(float));
        T[] items = new T[count];
        int i = 0;
        if (table != null)
        {
            foreach (Slot slot in table)
                if (!slot.Empty)
                    items[i++] = slot.item;
            info.AddValue("items", items, typeof(T[]));
        }
        else
            info.AddValue("items", null, typeof(T[]));
    }

MihaMarkic wrote Dec 23, 2007 at 7:44 PM

Duh, I see that fix is already posted :-)

Minimalist360 wrote Oct 4, 2011 at 3:30 PM

can this be worked into the source? moderator?

wrote Feb 21, 2013 at 10:40 PM