rspec/rules/S3366/description.adoc

8 lines
841 B
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
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.
2020-06-30 12:48:39 +02:00
2021-02-02 15:02:10 +01:00
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.
2020-06-30 12:48:39 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
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