Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

std::shared_ptr destructor

Hi, I have std::map of shared pointers,
Shared pointers are created and destroyed in main thread, but objects which pointer holds are used in multiple threads.
Is there are possibility when shared pointer is destroyed in main thread it destroys object which hi holds in other thread.
I mean that destructor of object which shared pointer holds is called from other thread then main where shared pointer is destroyed?
Since it's a pointer, yes, if memory is cleared in main thread, other threads would be pointing to empty memory as well
Hack the World!
(Dec 14, 2017 11:11 AM)0x45 Wrote:  Since it's a pointer, yes, if memory is cleared in main thread, other threads would be pointing to empty memory as well

I removed shared pointers from project to be sure.
Of course, I know that other threads will point to an empty memory, but now for knowledge purpose.
I am sure destructor will be called only once, but are you saying that it is not predictable in what thread destructor will be called if shared pointer is destroyed in main thread?
if you have del p_thread in main thread then destructor for that pointer will be called only in main thread.
Hack the World!
Thanks, I think the same.

For some strange reason I catch my destructor is called from other thread then main, so it comes to my mind is there are something about shared pointers that i do not know.
Other what comes to my mind is when assertions fail in other thread and abort() is called to close the program, probably in that case object do not have some predicted order in what thread they are destroyed.
Smart pointers are objects, there's nothing special about them, they are local variables that are owned by a specific thread just like anything else, they only differ from regular pointers when it comes to the lifetime of that object and what happens when they go out of scope essentially. It's quite clear if you look up RAII.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by leetAug 16, 2022 11:36 AM 7 348 Aug 16, 2022 11:36 AM
Last Post: Sajuuk
[C] by SugawaraFeb 2, 2020 10:39 AM 8 297 Feb 2, 2020 10:39 AM
Last Post: m1n0r1tyrep0rt
[C] by pmtobefriendJul 29, 2018 07:23 PM 3 213 Jul 29, 2018 07:23 PM
Last Post: notsteve