Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What solutions are there for circular references?

When using reference counting, what are possible solutions/techniques to deal with circular references?

The most well-known solution is using weak references, however many articles about the subject imply that there are other methods as well, but keep repeating the weak-referencing example. Which makes me wonder, what are these other methods?

  • I am not asking what are alternatives to reference counting, rather what are solutions to circular references when using reference counting.

  • This question isn't about any specific problem/implementation/language rather a general question.

like image 287
OB OB Avatar asked Jul 01 '09 14:07

OB OB


People also ask

How do you stop there are one or more circular references?

Click the "Formulas" tab in the ribbon menu at the top of the Excel window. Click the small arrow next to the "Error Checking" button in that area. Move your mouse over "Circular References" and the last entered circular reference will appear. Click on this reference to jump to that cell on the spreadsheet.

What is meant by circular reference problem and how would you solve it?

Millions of people using Excel don't get why they see the “circular reference” error message right after they've entered a formula. The message means that your formula is trying to calculate its own cell–kind of like when a dog chases its own tail.

What is the fastest way to find circular references?

Manually detect Circular References in ExcelGo to tab 'Formulas', choose 'Error-checking' and 'Circular References'. Excel will show you exactly in which cell(s) circular references are detected.

How do financial models avoid circular references?

In financial models, the best way to avoid circular references is to build in a “switch” that lets the user toggle between Average Balances and Beginning Balances when calculating items like Interest Income and Interest Expense.


2 Answers

I've looked at the problem a dozen different ways over the years, and the only solution I've found that works every time is to re-architect my solution to not use a circular reference.

Edit:

Can you expand? For example, how would you deal with a parent-child relation when the child needs to know about/access the parent?OB OB

As I said, the only good solution is to avoid such constructs unless you are using a runtime that can deal with them safely.

That said, if you must have a tree / parent-child data structure where the child knows about the parent, you're going to have to implement your own, manually called teardown sequence (i.e. external to any destructors you might implement) that starts at the root (or at the branch you want to prune) and does a depth-first search of the tree to remove references from the leaves.

It gets complex and cumbersome, so IMO the only solution is to avoid it entirely.

like image 150
Randolpho Avatar answered Jan 02 '23 05:01

Randolpho


Here is a solution I've seen:

Add a method to each object to tell it to release its references to the other objects, say call it Teardown().

Then you have to know who 'owns' each object, and the owner of an object must call Teardown() on it when they're done with it.

If there is a circular reference, say A <-> B, and C owns A, then when C's Teardown() is called, it calls A's Teardown, which calls Teardown on B, B then releases its reference to A, A then releases its reference to B (destroying B), and then C releases its reference to A (destroying A).

like image 41
Alex Black Avatar answered Jan 02 '23 04:01

Alex Black