Questions tagged [cyclic-reference]

A cyclic reference is established if object A holds a reference to B while B holds a reference to A.

A cyclic reference is established if object A holds a reference to B while B holds a reference to A. This exposes a dynamic problem ("hen-vs-egg"), for instance in a programming language, to create a cyclic reference, A and B must be known prior to establishing their inter-relationship. Thus a form of temporal sequence (delay) is needed if data structures are purely functional, for example by declaring the initialization of the references lazy. Cyclic references can also pose problems to garbage collection which needs to detect that none of two objects involved in a cyclic reference are reachable any more by the rest of the code.

143 questions
546
votes
16 answers

What happens when using mutual or circular (cyclic) imports?

In Python, what happens when two modules attempt to import each other? More generally, what happens if multiple modules attempt to import in a cycle? See also What can I do about "ImportError: Cannot import name X" or "AttributeError: ... (most…
Xolve
  • 22,298
  • 21
  • 77
  • 125
88
votes
5 answers

shared_ptr and weak_ptr differences

I am reading Scott Meyers "Effective C++" book. It was mentioned that there are tr1::shared_ptr and tr1::weak_ptr act like built-in pointers, but they keep track of how many tr1::shared_ptrs point to an object. This is known as reference counting.…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
35
votes
4 answers

What is a reference cycle in python?

I have looked in the official documentation for python, but i cannot seem to find what a reference cycle is. Could anyone please clarify what it is for me, as i am trying to understand the GC module. Thank you in advance for your replies.
IT Ninja
  • 6,174
  • 10
  • 42
  • 65
33
votes
6 answers

Java Enums: Two enum types, each containing references to each other?

Is there a way to get around the class-loading issues caused by having two enums that reference each other? I have two sets of enumerations, Foo and Bar, defined like so: public class EnumTest { public enum Foo { A(Bar.Alpha), …
Sbodd
  • 11,279
  • 6
  • 41
  • 42
25
votes
5 answers

Is cyclic dependency between anonymous class and parent class wrong?

I have following snippet of code: public class Example { private Integer threshold; private Map history; protected void activate(ComponentContext ctx) { this.history = Collections.synchronizedMap(new LinkedHashMap
24
votes
5 answers

Cycle in the struct layout that doesn't exist

This is a simplified version of some of my code: public struct info { public float a, b; public info? c; public info(float a, float b, info? c = null) { this.a = a; this.b = b; this.c = c; } } The…
alan2here
  • 3,223
  • 6
  • 37
  • 62
14
votes
12 answers

What is a cyclic data structure good for?

I was just reading through "Learning Python" by Mark Lutz and came across this code sample: >>> L = ['grail'] >>> L.append(L) >>> L ['grail', [...]] It was identified as a cyclic data structure. So I was wondering, and here is my question: What…
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
13
votes
2 answers

Data structures with cyclic dependencies in haskell

I'm trying to implement simple parser in haskell using parsec library (for learning purposes). So I wrote bunch of data structutes and related functions like this: data SourceElement = StatementSourceElement Statement |…
sergeyz
  • 1,308
  • 2
  • 14
  • 23
12
votes
4 answers

How to initialize and "modify" a cyclic persistent data structure in Scala?

I have searched and found some info on this topic but the answers are either confusing or not applicable. I have something like this: class Thing (val name:String, val refs:IndexedSeq[Ref]) class Ref (val name:String, val thing:Thing) Now, I want…
mentics
  • 6,852
  • 5
  • 39
  • 93
11
votes
5 answers

Prevent Cyclic references when converting with MapStruct

Today I started using MapStruct to create my Model to DTO converters for my project and i was wondering if it handled cyclic references automatically but it turned out it doesn't. This is the converter i made to test it: package…
valepu
  • 3,136
  • 7
  • 36
  • 67
11
votes
1 answer

Preventing cyclic reference memory leaks in Perl

I recently asked a question about overwriting objects and memory management in Perl. One of the answers I received notified me that I may have an issue with a script I recently wrote. I have a script with some very complex data structures that have…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
11
votes
4 answers

Maven says I have a cyclic reference in multi-module project but can't figure out why

I have a multi-module project that looks like this: module1 pom.xml module2 pom.xml pom.xml The pom.xml in module2 has a dependency on module1. When I run mvn clean compile I get the following error: The projects in the reactor contain a…
user977208
  • 603
  • 2
  • 5
  • 13
10
votes
3 answers

What is the explanation of null values in a cyclic dependency in final private enum fields?

Consider these enum declarations: enum Color { RED(Shape.CIRCLE), GREEN(Shape.TRIANGLE), BLUE(Shape.SQUARE); private final Shape shape; Color(Shape shape) { this.shape = shape; } Shape getShape() { …
mjn
  • 36,362
  • 28
  • 176
  • 378
9
votes
4 answers

How to properly handle a circular module dependency in Python?

Trying to find a good and proper pattern to handle a circular module dependency in Python. Usually, the solution is to remove it (through refactoring); however, in this particular case we would really like to have the functionality that requires…
Juan Carlos Coto
  • 11,900
  • 22
  • 62
  • 102
9
votes
2 answers

shared_ptr and cyclic references

I was trying with the cyclic references for boost::shared_ptr, and devised following sample: class A{ // Trivial class public: i32 i; A(){} A(i32 a):i(a){} ~A(){ cout<<"~A : "<
zeropoint
  • 235
  • 1
  • 4
  • 10
1
2 3
9 10