Hacker News new | ask | show | jobs
by ragnot 1032 days ago
This paper will help a lot: https://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf

tl;dr: Remember that the "new" operator does two operations: allocates memory and fills in the memory with the data. Now, if you have two threads (A & B):

- A allocates memory and then gets pre-empted

- B will pass the null pointer check and attempt to use the non-filled in memory block

1 comments

Thanks for the explanation!

That's the part I missed : the first null check can be skipped if the memory is allocated but the constructor hasn't been executed.