Print Page | Close Window

Stack Overflow Exception

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4601
Printed Date: 21-Aug-2025 at 6:41pm


Topic: Stack Overflow Exception
Posted By: cm
Subject: Stack Overflow Exception
Date Posted: 13-Nov-2013 at 11:05am
I call SaveChanges with a large number (tens of thousands) of changed entities. I get a stack overflow exception as shown in call stack below. In the past, other developers have worked around this problem by "chunking" the save but I was having problems when adding the entities to the manager with exceptions when adding entities with foreign keys to an entity saved several chunks previous. Thoughts anyone? Is there a documented limit on the number of entities that can be saved at one time?

     [Managed to Native Transition]   
>    mscorlib.dll!System.RuntimeType.GetGenericTypeDefinition() + 0x8f bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.CollectionDataContract.IsCollectionInterface(System.Type type) + 0x69 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle objectTypeHandle, System.Type objectType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) + 0x62 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x1a7 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x6f bytes   
     [Lightweight Function]   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.WriteXmlValue(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) + 0x31 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x6d bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x6f bytes   

... ... continues the same until:

     [Lightweight Function]   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.WriteXmlValue(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) + 0x31 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool verifyKnownType, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) + 0xeb bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle objectTypeHandle, System.Type objectType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) + 0x2c6 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x1a7 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x6f bytes   
     [Lightweight Function]   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) + 0x41 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x6d bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x6f bytes   
     [Lightweight Function]   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.WriteXmlValue(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) + 0x31 bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(System.Runtime.Serialization.XmlWriterDelegator writer, object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) + 0x11c bytes   
     System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.WriteObjectContentHandleExceptions(System.Runtime.Serialization.XmlWriterDelegator writer, object graph) + 0x3e2 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.SerializationFns.SaveDCS(object instance, System.IO.Stream stream, bool closeOnExit, System.Collections.Generic.IEnumerable<System.Type> knownTypes, bool useBinaryFormat) + 0x1c7 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.SerializationFns.Save(object instance, System.IO.Stream stream, bool closeOnExit, System.Collections.Generic.IEnumerable<System.Type> knownTypes, bool useBinary) + 0x62 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.SaveWorkState.DeepClone() + 0x86 bytes   
     IdeaBlade.EntityModel.Server.dll!IdeaBlade.EntityModel.Server.EntityServer.SaveChanges(IdeaBlade.EntityModel.SaveWorkState workState) + 0x78 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.EntityServerProxy.SaveChanges.AnonymousMethod__10() + 0x95 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.EntityServerProxy.ExecFunc<IdeaBlade.EntityModel.SaveWorkState>(System.Func<IdeaBlade.EntityModel.SaveWorkState> func, bool funcWillHandleException) + 0x6c bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.EntityServerProxy.ExecuteOnServer<IdeaBlade.EntityModel.SaveWorkState>(System.Func<IdeaBlade.EntityModel.SaveWorkState> func, bool funcWillHandleException) + 0x1f4 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.EntityServerProxy.SaveChanges(IdeaBlade.EntityModel.SaveWorkState workState) + 0xe2 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.EntityManager.SaveEntitiesCore(IdeaBlade.EntityModel.SaveWorkState workState) + 0xe6 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.EntityManager.SaveChangesAsyncCore.AnonymousMethod__aa(IdeaBlade.EntityModel.EntitySaveOperation op) + 0x111 bytes   
     IdeaBlade.EntityModel.dll!IdeaBlade.EntityModel.AsyncProcessor<IdeaBlade.EntityModel.EntitySaveOperation>.Execute.AnonymousMethod__4(object o) + 0xee bytes   
     mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x285 bytes   
     mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x9 bytes   
     mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x6f bytes   
     mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x1ea bytes   
     [Native to Managed Transition]   




Replies:
Posted By: kimj
Date Posted: 13-Nov-2013 at 2:30pm
The error is occurring when cloning your entities with the DataContractSerializer prior to the actual save to the database.  You might be able to work around the stack overflow by changing the "SerializationContextMode" to Alternate, as described here: http://drc.ideablade.com/xwiki/bin/view/Documentation/SerializationContext - http://drc.ideablade.com/xwiki/bin/view/Documentation/SerializationContext .
 
To deal with the chunking issue, the FindEntityGraph method on the EntityManager can help determine subsets of related entities.
 
Also, in the 6.1.15 release we changed how cloning is performed so that it no longer uses the DCS by default.   I don't know whether this would help with the stack overflow, but it might also be an option.
 



Print Page | Close Window