How to debug serialization problem??? Radial expansion of the byte stream...

How to debug serialization problem??? Radial expansion of the byte stream...

Old forum URL: forums.lhotka.net/forums/t/5889.aspx


vdhant posted on Wednesday, November 26, 2008

Hey guys
I am having a couple of small issues. I have created some base classes which i use to implement core functionality that I need on top of CSLA and have run into a slight problem.

I was doing some testing of some edge cases and found that if I have a list of say 100,000 items the test fails with an OutOfMememoryException. Now looking at when the problem occurs it happens in the test case when I clone the object, which will force a serialization/unserialization cycle.

Now i think this problem is not due to the size of the list (as Rocky has a similar test case and it runs fine) but due to radial expansion of the byte stream.

I have no idea how to go about finding the this problem as the base class is rather complex. So i was wondering if there is a tool/method that one can use to try and find the reference that is causing the issue. I must have a reference of the base back to itself or something similar that is not marked as NonSerialized but I can't seem to find it.

Any help would be great.
Cheers
Anthony

ajj3085 replied on Monday, December 01, 2008

Hmm... I think I would first check and see if you have any fields in your BO that reference other BOs.  If you're maintaining your own link from a child BO to a parent one, for example.  If you find those, you should mark them as NonSerializable and NotUndoable.  It sounds like there may be a circular reference somewhere.

vdhant replied on Monday, December 01, 2008

Humm unfortunately its not quite that simple...
Because of the additional functionality that I have added, its not as simple as finding a single field. On the other hand maybe it is, but i have looked through all of the fields and can't find where the issue is.

Hence why i was wondering if anyone know how to debug this problem. Maybe there is a way to output the object graph to a file so i can inspect graph and see where it is going wrong???

Does anyone know of anything like this or any other ideas???

Cheers
Anthony

vdhant replied on Wednesday, December 03, 2008

Any ideas??

RockfordLhotka replied on Wednesday, December 03, 2008

It is easy enough to output the serialized data to a file or something. Just look at the ObjectCloner code in CSLA - you'll see how it serializes the object into a MemoryStream, and then back out to create the clone.

You could create a similar method that serialized the object into a FileStream. Or that takes the contents of the MemoryStream and writes that to a file.

The challenge you'll have with the default BinaryFormatter is that the data is binary. You'll have to use a hex editor or something to try and make some sense out of it.

Another alternative is to try and use the NetDataContractSerializer. It may or may not have the same issue, and so it may or may not help you. But at least it produces XML and so you might have an easier time analyzing the serialized data to identify the problem.

vdhant replied on Wednesday, December 03, 2008

Humm... sounds like fun... Thanks for the help hopefully I will get something out of the file...

If I do go down the XML route, does it capture the object graph correctly??? As in there is an impedance mismatch between OO and XML, will I still be able to see what I need from it?

Cheers
Anthony

vdhant replied on Wednesday, December 03, 2008

Hi guys
I have just been able to dump out the object graph as XML. Just from quickly looking at it, there doesn't seem to be anything that jumps out at me. Is there an attribute or a node that I should be looking for that would point to a circular reference?
Cheers
Anthony

RockfordLhotka replied on Wednesday, December 03, 2008

There is a sort of impedance mismatch – though it has nothing to do with XML exactly.

 

When you serialize an object graph, with the intent of deserializing it to get an exact clone, you need to flatten all the references between objects in the graph.

 

In other words, you want each object’s state to exist in the byte stream exactly once. But at the same time, if that object is referenced from n other objects, you need to somehow retain those references so you can reestablish them on deserialization.

 

So the byte stream contains each object’s state once, but some form of encoding is used to demark references. In the case of NDCS this is done by assigning an arbitrary numeric value to each object in the graph, and then the references appear as a numeric placeholder.

 

Hopefully that makes sense? I think you’ll find the XML relatively easy to read for a simple object graph, but it gets more complex as you have more references between your objects.

 

Rocky

 

Copyright (c) Marimer LLC