rspec/rules/S3366/description.adoc

6 lines
811 B
Plaintext
Raw Normal View History

2020-12-23 14:59:06 +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
2020-12-23 14:59:06 +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
2020-12-23 14:59:06 +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