Infinite loop with a singleton – does this type of issue have a name?

I ran into an unusual error while working on my project. To better learn from and remember it, I’d like to know if this type of error has a name or some definition. (The error itself OutOfMemoryError isn’t unusual, I’m talking about what lead to this error).

My (simplified) situation was this:

I had a class Singleton:

class Singleton extends ClassA{
    private static Singleton instance;

    private Singleton(){ // implicitly calls super() }

    public static Singleton getInstance(){
        if (instance==null) instance = new Singleton();
        return instance;
    }
}

And it’s superclass ClassA:

abstract class ClassA{
    public ClassA(){
        ClassB objectB = new ClassB();
        // .. some logic ..
    }
}

And a class ClassB that uses Singleton in it’s constructor:

class ClassB{
    public ClassB(){
        Singleton singleton = Singleton.getInstance();
        // .. some logic ..
    }
}

What happens is the following:

1- ClassB is instantiated somewhere. It’s constructor is invoked and calls Singleton.getInstance().

2- Singleton.getInstance() sees that instance == null and executes instance = new Singleton().

3- The constructor of Singleton is run. It’s empty, but it implicitly calls the superclass’ (ClassA) constructor.

4- The superclass of Singleton instantiates a ClassB object new ClassB().

5- ClassB‘s constructor is invoked and calls Singleton.getInstance()

Since the instantiation of instance in Singleton never reached it’s finish, instance == null still returns true, and the cycle never ends.

Resulting in an infinite loop, that finally resulted in an OutOfMemoryError.

So my question is: is this kind of infinite-loop-with-singletons error a common issue? Any ideas how I can avoid it in the future?

14

It is a simple circular dependency during initialization, independent of the Singleton. There are three classes that instantiate each other in a cycle: A instantiates B, which instantiates C, which in turn instantiates A again, and you have an endless instantiation cycle.

Another thing that needs to be evaluated is why do the objects have a circular dependency on each other regardless of instantiation. That is, even if somehow they were all instantiated, they should not still have a circular dependency since that is often indicative of high coupling and possibly other design issues.

Having said that, it seems that the Singleton is not yet completely implemented, because it does not guarantee singleton across multiple threads. When the locking necessary for a singleton implementation is coded, it will very quickly run into a deadlock.

Usually, these types of errors are found:

  • At design time by looking at class interactions,
  • At implementation time by working through the code in the head,
  • At compile time as some compilers might detect and issue warnings/errors,
  • At runtime, by getting the kind of exception this code raised.

0

I can think of a couple of ways to break the cycle.

  1. Don’t instantiate a CMajor when instantiating MusicalResources. Let a higher level function construct the necessary Scales. Any time a Scale is constructed, add a reference to MusicalResources.

  2. Don’t query MusicalResources for the Notes when constructing an instance of CMajor. Query them on an as needed basis.

One of the downsides of Singletons is how they interact with super and sub classes. Another downside of Singletons is nesting them — that gets messy fast. No offense, but I find it a little funny when you say, “I don’t see the problem…” when in fact you are seeing a problem that blows up your runtime engine 😉

Aside from that, your model is all kitty-wampus. Again, no offense, but…! I think, for example, you’re making an equivalence between a Scale and a Note that messes up the hierarchy. I’d take out the Singleton code first, and then step back and make a diagram of the is-a and has-a relationships (for example, it might be cleaner if Scale “has a” Note instead of “is a” Note)

It might be good to diagram it out, with the classes, subclasses, and instances laid out with arrows between them.

If you’re dishing out immutable reference objects, bundling them up as public static final members of some container class is a lot cleaner and less nastiness-prone than Singleton. Also, I’m not sure where, but I think that enums are going to be your friends here too.

(Enums are kind of the Java-approved version of immutable Singleton, again without the nastiness.)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật