[CSHARP-416] StackoverflowException occours when I serialize an object witha nhivbernate persistentbag Created: 27/Mar/12  Updated: 02/Jun/22  Resolved: 02/Apr/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.3.1
Fix Version/s: 1.4.1

Type: Bug Priority: Major - P3
Reporter: Gian Maria Ricci Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 64 bit


Issue Links:
Related
related to CSHARP-424 When attempting to serialize an objec... Closed

 Description   

I'm usingn mongo for logging, when I try to call ToBsonDocument on an object that is mapped with nhibernate and has a property of type IList<T>, nhibernate will create a PErsistentGenericBag<T>, and it cause a StackOverflowException when I call ToBsonDocument

The exact type of the property is
Name = "PersistentGenericBag`1" FullName = "NHibernate.Collection.Generic.PersistentGenericBag`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"} System.Type

{System.RuntimeType}

 Comments   
Comment by Gian Maria Ricci [ 05/Apr/12 ]

Thanks a lot for the quick fix , really good work.

Comment by Robert Stam [ 02/Apr/12 ]

CSHARP-424 has been implemented, so I'm closing this ticket as fixed also.

Comment by Robert Stam [ 30/Mar/12 ]

Thank you for your comment. I have created a new ticket CSHARP-424 for an improvement that serializing an object with circular references should result in a catchable exception instead of a fatal StackOverflowException.

Comment by Gian Maria Ricci [ 30/Mar/12 ]

I understand these limitation, but StackOverflowException is not catchable, and it will kill your process immediately. Since I'm using MongoDb as a storage for log4net logs, to save object attached to logs, this problem makes impossibile to adopt this solution, because if someone try to log an object that has a circular reference the program shut down without any possibility to do a Try/Catch.

It would be nice, for circular references, to throw some specific exception, or at least a catchable one, or being able to specify to the serializer a maximum deep of serialization.

Thanks for your attenction.

Comment by Robert Stam [ 27/Mar/12 ]

Not all classes can be serialized by the BsonClassMapSerializer, which works with classes that meet the following requirements:

1. The class has a public no-argument constructor
2. All values to be serialized are exposed via get/set properties
3. All values to be serialized are in turn serializable
4. There are no circular references in the object graph to be serialized

Apparently NHibernate's PersistentGenericBag doesn't meet these requirements and is therefore not serializable by the BsonClassMapSerializer. A StackOverflow during serialization usually indicates a circular reference in the object graph.

Generated at Wed Feb 07 21:36:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.