0

I have a third party library which has a lot of c-style raw pointer. Now i am writing a thread in c++ 17. i want to use weak pointer for c style pointers of library. this library can be used by many other thread which modify/delete same pointers. how it can be achieved? is there any design pattern which can help?

In library :

struct table *table = (struct table *) malloc(sizeof(table));

Now in my thread:

auto wp = std::make_weak<struct table* >(table)
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 3
    Why a weak pointer? Why not a shared pointer? And inside your application, perhaps you should write a wrapper-class for the library (with all access to the library should go through the wrapper-class, and not directly)? – Some programmer dude Jan 09 '19 at 13:20
  • 1
    A weak ptr cant hold a raw pointer. That's the whole idea behind a weak ptr. It only points to a different type smart pointer that manages ownership and deletion. – Hatted Rooster Jan 09 '19 at 15:31
  • 2
    Perhaps you should first start out with determining who owns the pointers versus who is referring to the pointed to objects but doesn't actually own the pointers and the objects being pointed to. See [When is std::weak_ptr useful?](https://stackoverflow.com/questions/12030650/when-is-stdweak-ptr-useful) as well as [shared_ptr and weak_ptr differences](https://stackoverflow.com/questions/4984381/shared-ptr-and-weak-ptr-differences) as the answers to these have some interesting discussions about pointer ownership. – Richard Chambers Jan 09 '19 at 15:39
  • Consider using `std::unique_ptr`. Only use `std::shared_ptr + std::weak_ptr` when nothing else does. – Maxim Egorushkin Jan 09 '19 at 16:32

1 Answers1

0

One of the fatal problems with owing raw pointers, is that there is nothing you can do to observe that somewhere else has invalidated your pointer. Smart pointers work by tracking who knows about the object, and only destroying when the last pointer is destroyed.

std::weak_ptr is a complement to std::shared_ptr and nothing else. It does not magically know when delete is called on the object it is pointing to, it uses the same mechanism that std::shared_ptr does to know about the object's lifetime. Notice there is no constructor of std::weak_ptr that accepts a raw pointer.

If the library is managing the ownership, it is incorrect for you to put them into a smart pointer that uses std::default_delete. The best you can do is use the library's release function as the deleter of your smart pointers, and wrap the create function so that you return such a smart pointer instead of the raw one.

Caleth
  • 52,200
  • 2
  • 44
  • 75
  • Terminology: "when the last pointer" last owner? last `shared_ptr`? last strong ref? – curiousguy Jan 09 '19 at 17:49
  • @curiousguy for both `std::shared_ptr` and `std::unique_ptr`, the last owning pointer – Caleth Jan 09 '19 at 18:30
  • OK ... although neither `std::shared_ptr` nor `std::unique_ptr`, they are classes. Only pointers (scalars) are really pointers. – curiousguy Jan 09 '19 at 18:39
  • @curiousguy if you are going to be pedantic, they are *templates*. But they are templates for classes with pointer semantics, which is sufficient for a colloquial discussion, especially when using the qualifer "raw pointers" for things shaped `T*` – Caleth Jan 09 '19 at 18:43
  • Not trying to be "pedantic", but these so called "smart pointers" don't even have a conversion to a real pointer. (Also, almost all use practical uses of a `std::shared_ptr` are going to be on specializations of the template, that's why I mentioned classes not templates.) – curiousguy Jan 09 '19 at 18:53