8 lines
841 B
Plaintext
8 lines
841 B
Plaintext
In single-threaded environments, the use of ``++this++`` in constructors is normal, and expected. But in multi-threaded environments, it could expose partially-constructed objects to other threads, and should be used with caution.
|
|
|
|
|
|
The classic example is a class with a ``++static++`` list of its instances. If the constructor stores ``++this++`` in the list, another thread could access the object before it's fully-formed. Even when the storage of ``++this++`` is the last instruction in the constructor, there's still a danger if the class is not ``++final++``. In that case, the initialization of subclasses won't be complete before ``++this++`` is exposed.
|
|
|
|
|
|
This rule raises an issue when ``++this++`` is assigned to any globally-visible object in a constructor, and when it is passed to the method of another object in a constructor
|