Skip to main content

Questions tagged [smart-pointers]

An abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking

-1 votes
1 answer
66 views

Is returning a reference to a managed memory a malpractice?

Given this code: class Clazz { private: std::shared_ptr<int> something; public: Clazz() : something(std::make_shared<int>(0)) {} int &getSomething() { return *something; ...
tjzel's user avatar
  • 115
2 votes
1 answer
72 views

How to return smart pointers and covariance in C++

I am following this tutorial to understand how to return smart pointers and covariance in C++. #include <memory> #include <iostream> class cloneable { public: virtual ~...
GPrathap's user avatar
  • 7,650
2 votes
3 answers
94 views

Why does the Rust compiler drop unused variables in the reverse order they were declared?

In the Rust book an example is provided in chapter 15 section 3 to show when Rust runs the drop function on the variables c and d. struct CustomSmartPointer { data: String, } impl Drop for ...
Anthony Ruiz's user avatar
0 votes
0 answers
61 views

Build error of "No matching constructor for initialization" when initialize share_ptr<T[]> from unique_ptr<T[]>

First, I read this answer to realize why we want to convert an unique_ptr to a shared_ptr sometimes. Then, I follow the instructions of the converting constructor on cppreference shared_ptr. In C++11 ...
Rock's user avatar
  • 1
0 votes
1 answer
59 views

How to Implement operator-> for a Custom UniquePtr Class in C++? [duplicate]

I am trying to write my own implementation of unique_ptr in C++, and here is my UniquePtr header file: #include <iostream> template <typename T> class UniquePtr { T* ptr; public: ...
user25687822's user avatar
1 vote
0 answers
108 views

C++ std::weak_ptr::lock() seems to be executed at the same time as the destructor

I have a scenario where I need to use weak_ptr to cache resource, and use shared_ptr externally to determine the life cycle of the resource. When applying for this resource, I must ensure that it was ...
x f's user avatar
  • 83
0 votes
0 answers
79 views

Exception when handling std::shared_ptr and using std::dynamic_pointer_cast

I currently have this problem with a std::shared_ptr. Or at least, I think the problem is there. I have an app whose input data are passed through an interface from the calling program to the app ...
egev's user avatar
  • 41
2 votes
2 answers
80 views

Strange behavior when passing a cable captures `std::initializer_list<std::shared_ptr<Conversation>>` to the complete handler

The code snippet below always close the connection at once when a client has connected to the server, which is really out of my expection. #include <iostream> #include <memory> #include &...
John's user avatar
  • 3,348
0 votes
1 answer
45 views

Data race in the code snippet mentioned in other SO which read/write could not be invoked in the same time

The code below is seen at this SO except the comment in the original question has been removed. std::weak_ptr<int> g_w; void f3() { std::shared_ptr<int>l_s3 = g_w.lock(); //2. here ...
John's user avatar
  • 3,348
2 votes
1 answer
59 views

How std::weak_ptr::lock() is implemented to garantee both shared_ptr and weak_ptr can be used from threads without further synchronization

How std::weak_ptr::lock() is implemented to garantee both shared_ptr and weak_ptr can be used from threads without further synchronization(see the example code). As per the cppreference, which says ...
John's user avatar
  • 3,348
4 votes
0 answers
76 views

Why the compiler still complains even if std::mutex has been used when `-fsanitize=thread -ltsan` are enabled?

Why the compiler complain about this code snippet when -std=c++11 -lpthread -fsanitize=thread -ltsan are enabled? #include <iostream> #include <memory> #include <thread> #include <...
John's user avatar
  • 3,348
0 votes
0 answers
68 views

C++/WinRT idiomatic way to dispose of smart pointer class fields

Say I have a basic WinRT class: // midl runtimeclass Class: IClosable { Class(); } // .h struct Class : ClassT<Class> { Class() = default; void Close(); ~Class(); private: ...
Blindy's user avatar
  • 66.7k
0 votes
0 answers
42 views

Replicating the `std::unique_ptr` behavior of non-dependence for custom pointer wrappers

I'm writing a pointer wrapper class and want to replicate the following (great) functionality that std::unique_ptr offers of allowing us to create a complete type from an incomplete one (in any ...
Thornsider3's user avatar
0 votes
2 answers
136 views

If unique_ptr frees its pointer when destroyed, how am I supposed to know if other pointers to the same memory are still valid?

I'm currently struggling understanding the purpose of unique_ptr. I would like to have some kind of reference somewhere else of the content of std::unique_ptr<Component> component stored inside ...
Federico Dell'Aquila's user avatar
1 vote
1 answer
46 views

LLDB: Set breakpoint on std::shared_ptr<T> destructor

How can one set a symbolic breakpoint in lldb on ~shared_ptr, the destructor? Context: We wanted to break there to inspect a stack overflow of a naïvely implemented forward linked list in a demo ...
ctietze's user avatar
  • 2,902

15 30 50 per page
1
2 3 4 5
190