CodeSmith Community
Your Code. Your Way. Faster!

Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

Latest post 08-28-2008 11:13 AM by blake05. 18 replies.
  • 03-28-2007 8:22 AM

    • janiv
    • Not Ranked
    • Joined on 01-04-2007
    • Posts 7
    • Points 185

    Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi!

    StackOverflowException problem
    This is due to the clone() implementation in entity classes not being able to detect cyclic references during deep copy of an object tree. This ends up with a StackOverflowException when DeepLoading or DeepSaving, and sometimes when a collection (an instance of TList<T>) is set as a datasource in list controls.

    The attached patch file solves this by making sure that an object in the source tree is cloned only once. If the same source object is found, another place in the object tree, a reference to the first copy will be used instead of making a new copy. This is done by maintaining a dictionary with the objects already copied. This dictionary will be created in a entity at the clone() or copy() invocation, and then passed on to the nested copy methods. Please see source for details.

    The patch/solution includes a new interface, ICloneableEx, defined in the template IEntity.cst. This interface should probably be placed in its own template file (?).

    InvalidCastException in Entity.Equals method
    Suggested correction of this bug is also included in this patch.

    The patch is based on revision 528.

    Note
    The patch is not tested against the Northwind database as advised! 

     

    Regards
    ---
    Jan Ivar Vik and Tor-Odd Connelly
    Fundator AS (http://www.fundator.no)

    • Post Points: 65
  • 03-30-2007 4:55 AM In reply to

    • janiv
    • Not Ranked
    • Joined on 01-04-2007
    • Posts 7
    • Points 185

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi!

    In case you are going to use anything from our patch...

    Me and my colleague have been discussing the effect of the override of the Entity.Equals method (Actually, it was my colleague that realized the effect first - not me :-)). And there is a small issue with our patch that should probably be addressed in the final fix;

    The correct implementation of a clone method is to make it return a complete copy / clone of the source tree - exact. This is what the patch actually did until the Entity.Equal method was overridden. We just didn't see the consequences of the method being overridden, first. I'll try to explain: 

    Let's say the source tree we are about to clone, somehow would contain two instances of the same type (for instance the Person type):

              obj1 : Person
            /
    objx
            \
              ob2 : Person

    ...and those two Person objects contained the same values - making them equal except for the references from objx. The only thing that do differentiate those two objects are the references. With the patch above, calling clone() on objx will now produce a copy looking like this:

    objxCopy = obj1Copy : Person

    where objxCopy will contain two references to same person object. (The equal sign (=) now represents two distinct references, both pointing to the same object). The reason for this is:

    -When the clone logic checks if an object has been copied before, it calls Contains(objext y) on its dictionary of sourceobjects already copied (Check the line 59 in the patch file for an example of the contains method being used.). The dictionary then calls the Equals() method on each of the items in the dictionary to find whether the source object actually exists as a key in the dictionary. The new implementation of the Entity.Equals() method evaluates the actual values of the object, and returns true if the values are equal - while the old one only evaluated the object references. Due to this change in the Equals method, the same copy will be used for both the references in the target tree. In our case we want to evaluate the object references, not the values in the object.

    The clone implementation must always return a exact copy of the source which is not the case after the Entity.Equals() is overridden. And we can't just change the Entity.Equals() method of course. So the best solution we could come up width, was to create our own implementation of the dictionary used for holding the existing copies, by extending the Hashtable class, for instance. Then provide a new implementation of the Contains() method, and make it to use the Object.ReferenceEquals(object a, object b) instead of using the entity.Equals(object x) during its search for a match.

    Hope this was understandable.

     

    -Jan Ivar

    • Post Points: 5
  • 04-01-2007 5:21 PM In reply to

    • maqster
    • Top 500 Contributor
    • Joined on 03-23-2007
    • Posts 11
    • Points 325

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi,

    how do I apply this patch?

     /maqster

    • Post Points: 35
  • 04-02-2007 2:33 AM In reply to

    • janiv
    • Not Ranked
    • Joined on 01-04-2007
    • Posts 7
    • Points 185

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi!

    Check the post from Ornithopter, Mar 28, 2007, 11:33 PM in this thread:

    http://community.codesmithtools.com/forums/thread/23391.aspx

     


    -Jan Ivar

    • Post Points: 35
  • 04-02-2007 11:38 AM In reply to

    • Lex
    • Top 50 Contributor
    • Joined on 09-28-2006
    • Posts 72
    • Points 1,945

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi,

    This patch created a gigantic mess in my solution because sometimes, I DO want to create multiple copies of the same entity.
    If so, how should I proceed ? When should I use Copy and Clone ?
    Wouldn't it be better that what you did is NOT the default behaviour of the Copy ?

    • Post Points: 35
  • 04-03-2007 3:52 AM In reply to

    • janiv
    • Not Ranked
    • Joined on 01-04-2007
    • Posts 7
    • Points 185

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi!

    Not sure what you mean, really... but I think you have misunderstood something.

    the fix is supposed to make the clone method do what it really should do; make an exact copy of the object you want to clone. Not multiple copies, but one copy identical to the source object each time you call clone!. And when I say copy, I mean a real copy - where the copy does not share any object references with the source! If you want a number of copies, then call clone as many times as the number of copies you need...

    Now, the second post states that due to the change of the Equals method, the fix does not make an exact copy in all circumstances! So to make it work, we now have to change how the copy/clone logic compares the objects during the clone process.

     

    -Jan Ivar

    • Post Points: 5
  • 04-18-2007 2:37 PM In reply to

    • janiv
    • Not Ranked
    • Joined on 01-04-2007
    • Posts 7
    • Points 185

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi!

    This patch has does not conflict with the Equals method.

    And second - I think this patch actually fixes the problem I tried to explain in my second post.

    And, for those of you which keep asking me whether this patch will be integrated into the NetTiers templates; I do not know. No one from the team has made any response to me on the patch. I guess they are all busy with other tasks Smile.

    Also, remember that this patch has not been tested on the Northwind database, and I think some code (the new interface introduced) should be placed into its own template, as well.

    So until then, I think you need to use this patch, or create one yourself.

     

    -Jan Ivar

    • Post Points: 35
  • 04-20-2007 4:11 AM In reply to

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    we are doing some more testing with the patch and if it is stable we'll include it very soon!  Thanks for the contribution.

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 04-30-2007 10:09 AM In reply to

    • Trinity
    • Not Ranked
    • Joined on 04-18-2007
    • Posts 6
    • Points 150

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Any progress on this one Robert?

    • Post Points: 35
  • 05-10-2007 12:39 PM In reply to

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Yeah - is there any progress yet? It would be great to have this patch included in a new version as I've not been able to successfully apply the patch yet.

    Any more pointers otherwise?

    • Post Points: 35
  • 05-11-2007 3:19 PM In reply to

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi, I just updated this patch to rev 559. Hope it helps.

    Cya 

    Best regards, Nosferatus
    • Post Points: 35
  • 05-20-2007 2:36 AM In reply to

    • MrSmiley
    • Top 500 Contributor
    • Joined on 03-20-2007
    • Posts 17
    • Points 365

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Love the patch but just to let you know Tony's patch here: http://community.codesmithtools.com/forums/thread/24656.aspx breaks what your've done in EntityInstanceBase.generated

    • Post Points: 5
  • 09-20-2007 3:13 PM In reply to

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hi, Ive updated this patch for rev 642.


     

    Best regards, Nosferatus
    • Post Points: 35
  • 10-31-2007 10:53 AM In reply to

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

    Hmm, tried downloading the latest nightly build (rev 662), which didn't seem to solve my problem.

    • Post Points: 35
  • 11-26-2007 7:26 AM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 922
    • Points 34,710

    Re: Patch for DeepLoad/DeepSave StackOverflowException problem and InvalidCastException in Equals method

     Nosferatus,

    Any chance you can bring this up to the latest release?  If so I'll apply it.

    Thanks

    swin 

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
Page 1 of 2 (19 items) 1 2 Next > | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems