top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C++: Is it necessary when shared pointer goes out of scope then only destructor of the pointed object gets called ?

0 votes
498 views
C++: Is it necessary when shared pointer goes out of scope then only destructor of the pointed object gets called ?
posted Oct 2, 2020 by Crazy

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

It is not necessary destructor of an object will be called only if shared pointer goes out of scope. If you call reset() over the shared pointer object, it call destructor.

answer Oct 22, 2020 by Vimal Kumar Mishra
Similar Questions
+1 vote

I have a need for certain functions in a shared library to know the path to that library in order to find various resources. What I have in source.cpp:

#include 
#include 
static std::string myPath;

__attribute__((constructor))
void SetPath() {
 Dl_info dl_info;
 dladdr((void*)SetPath, 
 myPath = dl_info.dli_fname; // 

As the comment shows, when built with optimizations enabled, that line produces a segfault. With no optimizations, it works just fine. What magic must be done in order to ensure that this variable exists before the "constructor" function is called when loading the shared library?

0 votes

How does compiler work internally to make sure constant object can access only constant member function ?

...