[SalesForce] Maximum stack depth reached: 12

In our code, at some point we do

73: if(someObj.SomeExternalId__c.equals(activeContract.SomeExternalId__c))
74:     someObj2.activeContract__r = activeContract;
75: }

But it fails with "Maximum stack depth reached: 12". The debug logs contain this as its last part

15:46:41.322 (6322126573)|SYSTEM_METHOD_ENTRY|[73]|String.equals(Object)
15:46:41.322 (6322139791)|SYSTEM_METHOD_EXIT|[73]|String.equals(Object)
15:46:41.322 (6322145253)|STATEMENT_EXECUTE|[74]
15:46:41.327 (6327980176)|HEAP_ALLOCATE|[74]|Bytes:35
15:46:41.328 (6328096089)|SYSTEM_MODE_EXIT|false
15:46:41.328 (6328249333)|FATAL_ERROR|System.LimitException: Maximum stack depth reached: 12

We have been pulling hairs about possible loops, but we can't find anything for weeks now. It seems as if it fails at line 74 when it allocates memory for the variable assignment, but that should never trigger a stack depth error, right?

Does anybody have a clue?

Best Answer

Periodically this type occurs for us on the cs11 sandbox. Today is one such example. In the past I found if I refactor code, it will change which line the occurs and what depth it is reporting the error, but there is simply no way to resolve it. I think in our case it is related to some sort of bug when you call static methods to prepare values for a constructors super() call. It is only one set of classes where the error occurs. So far it has only been running test methods. If I report it to salesforce, by the time they get around into looking into it, the problem is gone. I have not seen this error on any other sandbox, even ones with the exact same code...