[Mono-bugs] [Bug 547484] New: I get an exception when trying to serialize an object with BinaryFormatter

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Thu Oct 15 21:35:44 EDT 2009


http://bugzilla.novell.com/show_bug.cgi?id=547484


           Summary: I get an exception when trying to serialize an object
                    with BinaryFormatter
    Classification: Mono
           Product: MonoTouch
           Version: unspecified
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Critical
          Priority: P5 - None
         Component: Runtime
        AssignedTo: gnorton at novell.com
        ReportedBy: bl at remobjects.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_1; en-us)
AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9

This class:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;

namespace MegaDemoMT
{
    public enum ChannelType
    {
        Http,
        Tcp
    }

    public enum HttpType
    {
        InternetPack,
        iPhone
    }

    [Serializable()]
    public class Settings : IDisposable
    {
        private static string fDocPath;
        private static string fRootPath;
        private static string fAppPath;
        private const string fFileName = "settings.dat";

        static Settings()
        {
            fDocPath =
Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            fRootPath = Path.Combine(fDocPath, "..");
            fAppPath = Path.Combine(fRootPath, "MegaDemoMT.app");        
        }

        protected Settings()
        {
            HttpUrlHistory = new List<string>();
            TcpServerHistory = new List<string>();
        }

        public static Settings Load()
        {            
            Console.WriteLine("Load settings");
            string lFilePath = Path.Combine(fDocPath, fFileName);
            if (File.Exists(lFilePath))
            {
                Console.WriteLine("Load from stream");
                using (FileStream lStream = new FileStream(lFilePath,
FileMode.Open)) {
                    BinaryFormatter lFormatter = new BinaryFormatter();
                    return lFormatter.Deserialize(lStream) as Settings;
                }
            }
            else
            {
                Console.WriteLine("Create new");
                Settings lSettings = new Settings();

                lSettings.ChannelType = ChannelType.Http;
                lSettings.HttpUrl = "http://127.0.0.1:8099/bin";
                lSettings.HttpType = HttpType.InternetPack;
                lSettings.HttpKeepAlive = true;
                lSettings.TcpServer = "127.0.0.1";
                lSettings.TcpPort = 8090;
                lSettings.Nagle = false;

                return lSettings;
            }
        }

        public List<String> HttpUrlHistory { get; private set; }

        public List<String> TcpServerHistory { get; private set; }

        public ChannelType ChannelType { get; set; }

        public string HttpUrl { get; set; }

        public HttpType HttpType { get; set; }

        public bool HttpKeepAlive { get; set; }

        public string TcpServer { get; set; }

        public int TcpPort { get; set; }

        public bool Nagle { get; set; }

        #region IDisposable implementation
        public void Dispose()
        {
            Console.WriteLine("Saving settings");
            BinaryFormatter lFormatter = new BinaryFormatter();
            string lFilePath = Path.Combine(fDocPath, fFileName);
            using (FileStream lStream = new FileStream(lFilePath,
FileMode.OpenOrCreate))
            {
                lFormatter.Serialize(lStream, this);
            }
            Console.WriteLine("Done");
        }
        #endregion

    }
}

results in an exception:

Unhandled Exception: System.InvalidProgramException: Invalid IL code in
System.Runtime.Serialization.Formatters.Binary.ObjectWriter:GetObjectData
(object,System.Runtime.Serialization.Formatters.Binary.TypeMetadata&,object&):
IL_0098: callvirt  0x06000375


  at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject
(System.IO.BinaryWriter writer, Int64 id, System.Object obj) [0x00000] 
  at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance
(System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject)
[0x00000] 
  at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects
(System.IO.BinaryWriter writer) [0x00000] 
  at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph
(System.IO.BinaryWriter writer, System.Object obj,
System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize
(System.IO.Stream serializationStream, System.Saving settings
Object graph, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize
(System.IO.Stream serializationStream, System.Object graph) [0x00000] 
  at MegaDemoMT.Settings.Dispose () [0x00000] 
  at MegaDemoMT.AppDelegate.WillTerminate (MonoTouch.UIKit.UIApplication
application) [0x00000] 

Reproducible: Always

Steps to Reproduce:
1.new MT app
2.add a field to the appdelegate class "Settings settings = Settings.Load():"
3.override WillTerminate, and put "settings.Dispose();" in it
4.run the app on the simulator, then hit the 'home' button to close the app 
Actual Results:  
got the exception as shown

Expected Results:  
write 'settings.dat' to the documents folder

-- 
Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.


More information about the mono-bugs mailing list