je5real / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

System.AccessViolationException when serializing a class with an interface reference to a struct that has a string member #322

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============
Email: MingyuTang@gmail.com

Got exception when running the following code. If I change Foo from struct to 
class, or change T.f from IFoo to Foo, the problem goes away.

//////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;
using System.IO;

namespace ProtoBufTest
{
    [ProtoContract, ProtoInclude(1, typeof(Foo))]
    public interface IFoo
    {
        string A { get; }
    }

    [ProtoContract]
    public struct Foo : IFoo
    {
        [ProtoMember(1)]
        public string a;

        public Foo(string a)
        {
            this.a = a;
        }

        public string A
        {
            get
            {
                return a;
            }
        }
    }

    [ProtoContract]
    public class T
    {
        [ProtoMember(1)]
        public IFoo f;
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                T t = new T();
                t.f = new Foo("a");
                Serializer.Serialize(ms, t);
            }
        }
    }
}
///////////////////////////////////////////

The following is the stack trace:

Unhandled Exception: System.AccessViolationException: Attempted to read or 
write protected memory. This is often an indication that other memory is 
corrupt.
   at System.Text.UTF8Encoding.GetByteCount(Char* chars, Int32 count, EncoderNLS baseEncoder)
   at System.Text.UTF8Encoding.GetByteCount(String chars)
   at ProtoBuf.ProtoWriter.WriteString(String value, ProtoWriter writer) in C:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 557
   at proto_4(Object , ProtoWriter )
   at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in C:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 45
   at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 400
   at ProtoBuf.ProtoWriter.WriteRecursionSafeObject(Object value, Int32 key, ProtoWriter writer) in C:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 65
   at proto_3(Object , ProtoWriter )
   at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in C:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 45
   at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 400
   at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in C:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 37
   at proto_1(Object , ProtoWriter )
   at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in C:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 45
   at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in C:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 400
   at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 141
   at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) in C:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 168
   at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance) in C:\Dev\protobuf-net\protobuf-net\Serializer.cs:line 78

   at ProtoBufTest.Program.Main(String[] args) in C:\Users\MingyuT\Documents\Visual Studio 2010\Projects\ProtoBufTest\Program.cs:line 51

What steps will reproduce the problem?
1. Serializer.Serialize
2.
3.

What is the expected output? What do you see instead?
Expecting no error. Got exception instead.

What version of the product are you using? On what operating system?
r480 on windows 7

Please provide any additional information below.

Original issue reported on code.google.com by MingyuT...@gmail.com on 22 Sep 2012 at 4:12