0

I want to recorded the line which created the shared_ptr in C++ 11. Here is my way to rewrite shared_ptr as Shared_ptr :

template<class T>
class Shared_Ptr{
public:
    Shared_Ptr(T* ptr = nullptr,int line=__LINE__)
        :_pPtr(ptr)
        , _pRefCount(new int(1))
        , _pMutex(new mutex)
    {
        cout<<this<<"is located in "<<line<<endl;
    }
    ~Shared_Ptr()
    {
        Release();
        cout<<this<<endl;
    }
    Shared_Ptr(const Shared_Ptr<T>& sp)
        :_pPtr(sp._pPtr)
        , _pRefCount(sp._pRefCount)
        , _pMutex(sp._pMutex)
    {
        AddRefCount();
    }
    Shared_Ptr<T>& operator=(const Shared_Ptr<T>& sp)
    {
        //if (this != &sp)
        if (_pPtr != sp._pPtr)
        {
            Release();
            _pPtr = sp._pPtr;
            _pRefCount = sp._pRefCount;
            _pMutex = sp._pMutex;
            AddRefCount();
        }
        return *this;
    }
    T& operator*(){
        return *_pPtr;
    }
    T* operator->(){
        return _pPtr;
    }
    int UseCount() { return *_pRefCount; }
    T* Get() { return _pPtr; }
    void AddRefCount()
    {
        _pMutex->lock();
        ++(*_pRefCount);
        _pMutex->unlock();
    }

    
private:
    void Release()
    {
        bool deleteflag = false;
        _pMutex->lock();
        if (--(*_pRefCount) == 0)
        {
            delete _pRefCount;
            delete _pPtr;
            deleteflag = true;
        }
        _pMutex->unlock();
        if (deleteflag == true)
            delete _pMutex;
    }
private:
    int *_pRefCount;
    T* _pPtr;
    mutex* _pMutex;
};
class student
{
int age;
public:
  student(int a):age(a)
  {

  }
}
;
int main()
{
   Shared_ptr<student> Tom(new student(24),__LINE__);
}

Is there a way to make Shared_ptr<student>Tom(new student(24)) as same as Shared_ptr <student> Tom(new student(24),__ LINE__) in C++11? In other words , invoke class Constructor with the arguments bound to args.

I tried to use marco to achieve,but I don't know the correct way how to define the macro of template class constructor.

Below is the macro definition I tried to write but wrong

template<typename T>
#define Shared_ptr<T>::Shared_ptr(T*) Shared_ptr<T>::Shared_ptr(T * ,__LINE__)
  • please show your attempt. Make it a [mcve] – 463035818_is_not_an_ai Oct 19 '22 at 07:46
  • 1
    @463035818_is_not_a_number: didn't the OP show his attempt ? –  Oct 19 '22 at 07:48
  • @YvesDaoust OP is asking how to use a macro and says they tried to use one. There is no such macro in the posted code – 463035818_is_not_an_ai Oct 19 '22 at 07:49
  • 1
    @463035818_is_not_a_number: should he add a line such as `#define MyMacro No idea how to do that` ? –  Oct 19 '22 at 07:51
  • @YvesDaoust no they should show what they tried, just what they did already – 463035818_is_not_an_ai Oct 19 '22 at 07:52
  • [For C++ 20 using source_location](https://stackoverflow.com/a/67971576/12002570). Also see [this](https://stackoverflow.com/a/67970107/12002570) where `__builtin_LINE()` is used. – Jason Oct 19 '22 at 08:00
  • 1
    @463035818_is_not_a_number: we probably didn't see the same versions of the post. –  Oct 19 '22 at 08:02
  • I want to know which line owns the shared_ptr which is causing the object to not be destructed. – Baron King Oct 19 '22 at 08:04
  • @user17732522 I think OP has added the code for `Shared_ptr`. – Jason Oct 19 '22 at 08:05
  • With the code you are showing now `Shared_ptr Tom(24);` doesn't work in the first place, so it is hard to follow what you want. – user17732522 Oct 19 '22 at 08:06
  • 1
    If you have a memory leak and need to figure out where it comes from, you should use an external debugging tool. For example valgrind or the address sanitizer (ASAN) which includes a leak detector. There are other leak detectors as well. – user17732522 Oct 19 '22 at 08:07
  • Sorry, the code is error. I have corrected it already. – Baron King Oct 19 '22 at 08:13
  • My problem is not a memory leak. my problem is like this https://stackoverflow.com/questions/1061634/how-do-i-know-who-holds-the-shared-ptr. I want to find which line has the object shared_ptr which is causing the object to not be destructed. – Baron King Oct 19 '22 at 08:16

1 Answers1

1

Replace int line=__LINE__ in constructor parameters with int line = __builtin_LINE(). It's a non-standard compiler extension, but it works at least in GCC, Clang, and MSVC (i.e. most common compilers).

Then Shared_ptr<student> Tom(nullptr); will work.


Shared_ptr<student> Tom(42); will not work, because Shared_ptr doesn't have the right constructor, but it has nothing to do with getting the line number.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207