3

I'm still playing with the newly released version of protobuf-net and I'm facing an issue I don't understand.

let's consider the piece of code below:

    [ProtoContract]
    class Node
    {
        public Node()
        {
            Children = new List<Node>();
        }

        [ProtoMember(1, IsRequired = true)]
        public int Data { get; set; }

        [ProtoMember(2, IsRequired = true, AsReference = true)]
        public List<Node> Children { get; set; }

        public void AddChild(Node child)
        {
            Children.Add(child);
        }
    }

    static void Main()
    {
        Node n = new Node {Data = 0}, root = n;
        for (int i=1; i<15; i++)
        {
            Node child = new Node {Data = i};
            n.AddChild(child);
            n = child;
        }
        Node clone = Serializer.DeepClone(root);
    }

It throws a exception of type ProtoException with the message "Possible recursion detected..."

The funny thing is that if I remove the attribute AsReference on the Children property it doesn't! Unfortunately, the lines above are just written to illustrate the problem and I need this attribute for the real structure I'm using.

So my question is... is it a known problem and is there any patch planned to fix it very soon? Or does anyone know any workaround?

Thanks

pierroz
  • 7,653
  • 9
  • 48
  • 60

1 Answers1

3

This is simply a bug (thank you for exercising the beta so thoroughly!) - in the dynamic/reference handling it was double-counting the object (once as part of the shim wrapper it spoofs to do the magic, and once for the object itself).

For efficiency, the recursion detection only kicks into full gear beyond a specific depth. Your code tripped this depth, causing the double-counting to be seen as recursion. I have fixed this in the code. The code above passes locally, and will be in the next drop.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • yeah! thanks a lot! I'm going to download and compile the sources to keep on bugbashing the new release :) – pierroz May 26 '11 at 06:24
  • sources downloaded and compiled: I confirm my bug is fixed!! thank you so much again EDIT: ah sorry I hadn't refreshed my screen and seen you r comment :P – pierroz May 26 '11 at 09:27
  • Hi Marc please share the latest link to download which has this as a bug – Aada Aug 06 '13 at 12:31
  • @Aada your best bet is to use the NuGet package manager in visual studio, which makes it easy to stay up to date; the NuGet package is simply "protobuf-net". Up to date downloads are also available separately on google-code: https://code.google.com/p/protobuf-net/downloads/list – Marc Gravell Aug 06 '13 at 12:32
  • @Aada "protobuf-net r640.zip" contains different folders for lots of different frameworks, including .net 2.0, .net 3.0, etc – Marc Gravell Aug 06 '13 at 12:48
  • @MarcGravell: Ya i took the dll from .net30 and referred it , i am getting an issue as "Possible recursion detected (offset: 2 level(s)): Node". ! – Aada Aug 06 '13 at 12:53
  • @Aada and again: it is probably correct. So : what does your model look like? – Marc Gravell Aug 06 '13 at 15:13
  • @MarcGravell: your answer in http://stackoverflow.com/questions/10070710/protobuf-net-possible-recursion-detected solved my problem :) Thanks a ton – Aada Aug 07 '13 at 05:03