That's because it's a WRAPPER function. It is NOT supposed to do any work. It's there ONLY because calling CreateThread requires a specific function prototype and can only pass by pointer.Here is the code again without the functions, exhibiting the same behavior:
Stop modifying it. It's not supposed to do anything other than call the other functions.
Do I really have to write a true sample to illustrate this problem?
The thing is that the thread want to use the same memory for some purpose (we're sharing data across threads).Even without the functions, you are sharing memory across threads, and one thread is deleting the memory while the other is using it.
There's no reason (except as a fun exercise) to try to ban use of references to your object.
Seeing as this is a safe pointer with a ref count, we're not deleting any data at all. Instead, what we're doing is decrementing the ref count. When the ref count reaches 0, it destroys the object.
The general rule is: For every instance of the object there is, the counter should increase with one. But it doesn't if you pass by reference of pointer.
What exactly is the sample doing?
It creates some memory that it wants the thread to get access to (because it needs it for some reason) and the main function should also use this memory to do some work with it.
The thread might be some sort of progress thread that reports progress to something by reading that data.
When the main function is done with whatever it's doing (does not need to report progress anymore, because it's finished), then it tells the class it doesn't need the memory anymore and the class recrements the ref pointer (and should naturally tell the thread it's done, as well).
The thread will in this case CRASH because the memory is freed (due to the object being passed by reference and not by value). If the object is passed by value, the ref counter will increase by one and will still be valid until the thread tosses it away too.