Search code examples
c#.netdebuggingserializationbinaryformatter

Binary formatter returns caller and cancels form.close


I want to serialize an array of structs with binary formatter to send it over network or save it to file in this case.
It is subscribed to the Form.Closing event of my Form

void writeHistoryToFile(object sender, CancelEventArgs e)
{
    ListView.ListViewItemCollection coll = historyListView.Items;
    int count = coll.Count;
    if(count == 0)
        return;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream memStr = new MemoryStream();
    searchResult[] container = new MainForm.searchResult[count];
    for(int i = 0; i < count; i++)
    {
        searchResult tagged = (searchResult)coll[i].Tag;
        container[i] = tagged;
    }
    byte[] bytesToWrite;
    bf.Serialize(memStr, container);   //HERE
    bytesToWrite = memStr.ToArray();   //BREAKPOINT

    List<FileInfo> hisFls = historyFiles;
    if(hisFls.Count != 0)
    {
        foreach(FileInfo element in hisFls)
        {
            element.Delete();
        }
    }

    FileInfo serFile = getTempFile(".avsh");
    using(FileStream writeStream = serFile.OpenWrite())
    {
        writeStream.Write(bytesToWrite, 0, bytesToWrite.Length);
        writeStream.Flush();
    }
}

What it does:
I close the form and the function gets called (breakpoints set above HERE are triggered).
When let running loose, the form doesn't close and becomes responsive again.
When stepping through the code, the debugger shortly hangs at HERE and then the code runs continuously again.
Breakpoints set behind HERE are never reached.

So it seems that the binary formatter returns the function or something, but without any Exception or such.

UPATE
Looks like my struct is not serializable, duh.
Added [Serializable] and it works now. See comments as to why there were no exceptions etc.. -_- Errormessage and stacktrace

The Type "ExportGrepper.MainForm+searchResult" in Assembly Arbeitsvorräte duchsuchen, Version=1.0.5351.15603, Culture=neutral, PublicKeyToken=null" is not marked as serializable.

   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)

   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)

   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)

   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)

   at ExportGrepper.MainForm.writeHistoryToFile(Object sender, CancelEventArgs e)

Solution

  • "Answer" to be able to close this question: Try adding a Try..Catch to your code and check which error occurs. Then make your struct serializable. ;)