Circular references in sObjects causes StackOverflowError
Trailblazer Community

Circular references in sObjects causes StackOverflowError

Apex , Winter 15

Last updated 2019-09-07 ·Reference W-2511387 ·Reported By 13 users

Fixed - Winter '20

If there is a circular reference in sObjects, an operation calling the equals() method or the hashCode() method to the sObject records results in a StackOverflowError.
The equals() method or the hashCode() method can be called when putting the sObject record to a Set/Map or if turning Apex Code Log Level into FINEST.

1) Create custom objects, say CircularRefObj1, CircularRefObj2.
2) On CircularRefObj1, create a lookup relationship field "obj2ref" which is related to CircularRefObj2.
3) On CircularRefObj2, create a lookup relationship field "obj1ref" which is related to CircularRefObj1.
4) Run the following apex code via workbench/dev console.
CircularRefObj1__c o1 = new CircularRefObj1__c();
insert o1;

CircularRefObj2__c o2 = new CircularRefObj2__c();
insert o2;

o1.obj2ref__r = o2;
o2.obj1ref__r = o1;

Set<sObject> s = new Set<sObject>();
s.add(o1); // call SObjectScriptRow.hashCode().
s.add(o1); // call SObjectScriptRow.hashCode() and equals().

You will see that the execution fails with the System.LimitException, as follows.

EXCEPTION: System.LimitException: Maximum stack depth reached: 1
STACKTRACE: AnonymousBlock: line 11, column 1

If you set the Apex Code Log Level to FINEST, you will see that the SystemLimitException occurs at line#8.

An sObject is represented as an SObjectScriptRow object in apex, and its field values are stored in a HashMap in a MapBasedRowProvider the SObjectScriptRow object has. When the sObject has a relationship field to another sObject, the HashMap in the MapBasedRowProvider holds a SObjectScriptRow object of the sObject to which the relationship field refers.

If the hashCode() method is called to the SObjectScriptRow object, it calculates a hash value by calling the hashCode() method to the HashMap in the MapBasedRowProvider.

And the HashMap calls the hashCode() method to each value. It eventually calls the hashCode() method to the SObjectScriptRow object of the related sObject.

In this way, if there is a circular reference in sObjects, they recursively call the hashCode() method each other, and consume stack memory.
The same is true in the equals() method.

No workaround.

Is it Fixed?

AP0 AP3 AP4 AP5 AP6 AP7 AP8 AP9 AP10 AP11 AP12 AP13 AP14 AP15 AP16 AP17 AP18 AP19 AP20 AP21 AP22 AP28 CS1 CS2 CS3 CS4 CS5 CS6 CS7 CS8 CS9 CS10 CS109 CS108 CS107 CS106 CS105 CS102 CS101 CS100 CS115 CS119 CS110 CS117 CS114 CS113 CS112 CS111 CS11 CS116 CS12 CS122 CS121 CS126 CS127 CS129 CS128 CS125 CS124 CS123 CS137 CS138 CS13 CS133 CS132 CS14 CS148 CS142 CS159 CS152 CS151 CS15 CS165 CS16 CS169 CS17 CS18 CS194 CS19 CS198 CS196 CS195 CS20 CS21 CS22 CS23 CS24 CS25 CS26 CS27 CS28 CS29 CS30 CS31 CS32 CS33 CS34 CS35 CS36 CS37 CS40 CS41 CS42 CS43 CS44 CS45 CS47 CS50 CS51 CS52 CS53 CS54 CS57 CS58 CS59 CS60 CS61 CS62 CS63 CS64 CS65 CS66 CS67 CS68 CS69 CS70 CS71 CS72 CS73 CS74 CS75 CS76 CS77 CS78 CS79 CS80 CS81 CS82 CS83 CS84 CS85 CS86 CS87 CS88 CS89 CS90 CS91 CS92 CS94 CS95 CS96 CS97 CS98 CS99 EU7 EU8 EU10 EU12 EU13 EU14 EU15 EU16 EU17 EU18 EU19 EU25 EU26 EU27 EU28 EU29 EU30 EU31 EU32 NA104 NA107 NA109 NA100 NA101 NA103 NA102 NA105 NA119 NA116 NA110 NA118 NA112 NA111 NA115 NA114 NA113 NA117 NA125 NA124 NA122 NA120 NA126 NA127 NA123 NA129 NA121 NA128 NA138 NA134 NA133 NA136 NA135 NA132 NA131 NA130 NA137 NA139 NA140 NA142 NA141 NA146 NA147 NA148 NA154 NA155 NA172 NA174 NA171 NA173 NA196 NA21 NA46 NA47 NA49 NA52 NA54 NA57 NA58 NA59 NA61 NA62 NA64 NA65 NA66 NA67 NA68 NA69 NA70 NA71 NA72 NA73 NA74 NA75 NA76 NA77 NA79 NA80 NA81 NA82 NA83 NA84 NA85 NA86 NA87 NA88 NA89 NA90 NA91 NA92 NA93 NA94 NA95 NA96 NA97 NA98 NA99 UM1 UM2 UM3 UM4 UM5 UM6 UM7

Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.