3

This exception is being thrown on my test application for serializing with ProtoBuf.

SceneryFile:temp.ad2
ProtoBuf.ProtoException: Possible recursion detected (offset: 2 level(s): SceneryFile: temp.ad2
at ProtoBuf.ProtoWriter.CheckRecursionStackAndPush(Object instance( in c:\protbuf-net\protobuf-net\ProtoWriter.sc:line321

I found these other questions

protobuf-net: Possible recursion detected

Protobuf-net possible recursion detected: serialize children and parents

The depth of my tree is only 4. I looked to see if I can change the ProtoBuf.ProtoWriter.RecursionCheckDepth but I could not find a way to do that in my code.

Based on Marc's answer to the last question I think it means by reference that the same object instance is being referred to again - so in my case the SceneryFile is referring to itself or is referred to somewhere else (actually it is the base class). It is in the sense that all the direct children hold a reference to it. The answer here seems to suggest this might be the problem:

Based on the findings of the second question above I removed the parent from being serialized and this did stop this problem.

Is there some way to allow me to keep the reference to the parent as is in the serialization or do I need to do something like use an index for the parent and then reconstruct the graph when I decompile?

Thanks

Community
  • 1
  • 1
ScruffyDuck
  • 2,606
  • 3
  • 34
  • 50

1 Answers1

3

The "recursion check depth" is an optimisation detail only: to avoid the overhead of constantly checking every object for recursion, it only checks if the depth is higher than this. It makes no difference to what is/isn't valid.

There are two approaches here. My preferred would be to just serialise "child" relationships, and use an after-deserialization callback that then loops over the children and sets their parent property. This is done via callback attributes, and is usually 5-ish lines of code.

The other is to tell protobuf-net to respect references (set AsReference to true on the members). This changes the layout, making it inappropriate if you want to interop to a different (non protobuf-net) implementation.

I can add examples of both ir either, but not on a phone!

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900