Hi I am making my own reference counting smart pointer, but before I start there are two concepts I don't quite understand.
I get that when a shared pointer is created to point to an object I must allocate memory for a struct/class that will contain information such as the reference count (initially one) and maybe a mutex as well for incrementing and decrementing. When I use, say the =operator to make another shared pointer also point to this object, I will also pass the pointer to this struct/class to that new pointer so I can increment the count. My question is, if I make a third shared pointer point to this object(not using the copy constructor or =operator), then this pointer won't know about the struct and therefore will have a reference count of 1, if I then delete the pointer, the count will reach 0 and the object will be destroyed when in fact, there are two other pointers for this object?
If a shared pointer has a reference count of 1, and then multiple threads are created, if one thread ends/destroys it, what happens with the other threads that may still be running?
I get that when a shared pointer is created to point to an object I must allocate memory for a struct/class that will contain information such as the reference count (initially one) and maybe a mutex as well for incrementing and decrementing.
Yes you will need a counter.
The mutex is only required if you plan on multithreading. I would concentrate on getting the counting working first worry about the locking afterwords.
When I use, say the =operator to make another shared pointer also point to this object, I will also pass the pointer to this struct/class to that new pointer so I can increment the count.
The point about shared pointer is that they take ownership of the pointer. Once you have created the shared pointer there should be no instances of a RAW pointer to the same object. So when you copy or assign you are doing it all through the shared pointer.
My question is, if I make a third shared pointer point to this object(not using the copy constructor or =operator), then this pointer won't know about the struct and therefore will have a reference count of 1.
Your assumption is correct. That is why when you create the shared pointer you should not keep a copy of the pointer. This is another reason why std::make_shared() was introduced, it allocates memory and immediately wraps it in a smart pointer thus no RAW pointer is returned to user code.
if I then delete the pointer, the count will reach 0 and the object will be destroyed when in fact, there are two other pointers for this object?
That is exactly the problem. This is why you should not create or pass around RAW pointers to objects that are already being managed.
If a shared pointer has a reference count of 1, and then multiple threads are created, if one thread ends/destroys it, what happens with the other threads that may still be running?
If your reference count is working and you only have shared pointers managing the RAW pointer it should work as expected. But if you delete it in one thread it is destroyed in all threads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With