Why do IDE’s complain about “leaking this in constructor”? I’ve always assumed that it’s just bad practice. But I actually never found why it is bad.
Why do IDE’s complain about leaking this in constructor? I’ve always assumed that it’s
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Leaking the
thisreference in the constructor (not controller) is dangerous, especially in a multithreaded environment. This is because the object is not fully constructed until the constructor call finishes. Leakingthisfrom the constructor thus means that the external world gets access to an object which is not yet fully constructed. This may not necessarily lead to problems in a a single-threaded program (although it is possible, but the problem is much more obvious in this case). But ifthisis leaked to other threads, they can actually try to do something with the object before its construction is finished, which leads to subtle and hard to find bugs.