smart-pointers

Find memory leaks caused by smart pointers

Does anybody know a "technique" to discover memory leaks caused by smart pointers? I am currently working on a large project written in C++ that heavily uses smart pointers with reference counting. Obviously we have some memory leaks caused by smart pointers, that are still referenced somewhere in the code, so that their memory does not ...

Smart Pointers: Or who owns you baby?

C++ is all about memory ownership Aka "Ownership Semantics" It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory. In C++ ownership is documented by the type a RAW pointer is wrapped inside thus in a good (IMO) C++ program it is very r...

What is a smart pointer and when should I use one?

What is a smart pointer and when should I use one? ...

Boost shared_ptr container question

Let's say I have a container (std::vector) of pointers used by a multi-threaded application. When adding new pointers to the container, the code is protected using a critical section (boost::mutex). All well and good. The code should be able to return one of these pointers to a thread for processing, but another separate thread could ...

Pointers and containers

We all know that RAW pointers need to be wrapped in some form of smart pointer to get Exception safe memory management. But when it comes to containers of pointers the issue becomes more thorny. The std containers insist on the contained object being copyable so this rules out the use of std::auto_ptr, though you can still use boost::sh...

boost::shared_ptr STL container question

Assume I have a class foo, and wish to use a std::map to store some boost::shared_ptrs, e.g.: class foo; typedef boost::shared_ptr<foo> foo_sp; typeded std::map<int, foo_sp> foo_sp_map; foo_sp_map m; If I add a new foo_sp to the map but the key used already exists, will the existing entry be deleted? For example: foo_sp_map m; vo...

auto_ptr or shared_ptr equivalent in managed C++/CLI classes

In C++/CLI , you can use native types in a managed class by it is not allowed to hold a member of a native class in a managed class : you need to use pointers in that case. Here is an example : class NativeClass { .... }; public ref class ManagedClass { private: NativeClass mNativeClass; // Not allowed ! NativeClass * mNativeCla...

Why shouldn't you use references to smart pointers?

I recall reading somewhere that using references to smart pointers can cause memory corruption. Is this simply because of using the reference of the smart pointer after its been destroyed? Or does the reference counting get messed up? Thanks for clarifying ...

How can I use covariant return types with smart pointers?

I have code like this: class RetInterface {...} class Ret1: public RetInterface {...} class AInterface { public: virtual boost::shared_ptr<RetInterface> get_r() const = 0; ... }; class A1: public AInterface { public: boost::shared_ptr<Ret1> get_r() const {...} ... }; This code does not compile. In visual stu...

Idiomatic use of std::auto_ptr or only use shared_ptr?

Now that shared_ptr is in tr1, what do you think should happen to the use of std::auto_ptr? They both have different use cases, but all use cases of auto_ptr can be solved with shared_ptr, too. Will you abandon auto_ptr or continue to use it in cases where you want to express explicitly that only one class has ownership at any given poin...

Really Strange Problem about access violation

I've met a really strange problem: The code is as follow: ::boost::shared_ptr<CQImageFileInfo> pInfo=CQUserViewDataManager::GetInstance()->GetImageFileInfo(nIndex); Image* pImage=pInfo->m_pThumbnail; if(pImage==NULL) pImage=m_pStretchedDefaultThumbImage; else { // int sourceWidth = pInfo->GetWidth(); int sourceHeight = pInfo->GetH...

Replacing auto_ptr in VC++ 8

std::auto_ptr is broken in VC++ 8 (which is what we use at work). My main gripe with it is that it allows auto_ptr<T> x = new T();, which of course leads to horrible crashes, while being simple to do by mistake. From an answer to another question here on stackoverflow: Note that the implementation of std::auto_ptr in Visual Studio 2...

C++ Smart Pointer performance

How much do using smart pointers, particularly boost::shared_ptr cost more compared to bare pointers in terms of time and memory? Is using bare pointers better for performance intensive parts of gaming/embedded systems? Would you recommend using bare pointers or smart pointers for performance intensive components? ...

What's the difference between BSTR and _bstr_t ?

Anyone can explain the difference between types mentioned above and some sample usage to clearly explain the difference between the two? Any help would be highly appreciated! Note: this question is a spin-off from this other question ...

Once you've adopted boost's smart pointers, is there any case where you use raw pointers?

I'm curious as I begin to adopt more of the boost idioms and what appears to be best practices I wonder at what point does my c++ even remotely look like the c++ of yesteryear, often found in typical examples and in the minds of those who've not been introduced to "Modern C++"? ...

smart pointers + "this" considered harmful?

In a C++ project that uses smart pointers, such as boost::shared_ptr, what is a good design philosophy regarding use of "this"? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-sta...

Smart pointers for Windows Mobile 6 SDK

I cannot get std::tr1::shared_ptr for my WinMobile project since the STL for WinCE is maintained by a different team at Microsoft :( aarrgh... Anyone worked with another thread-safe, reference counting smart pointers? I'm actually using yasper which seems to be good. Thank you very much. ...

smart pointers advice

Well I know there are good topics about smart pointers, but since I'm starting to use them extensively I want to know some recommendations on bad or dangerous uses, e.g: Raw-pointer conversions Smart pointers to references/References to smart pointers etc. Thank you in advance. (Added) Suppose I've a class A with pointers to B as m...

RAII and smart pointers in C++

In practice with C++, what is RAII, what are smart pointers, how are these implemented in a program and what are the benefits of using RAII with smart pointers? ...

Smart pointers with a library written in C

I'm using C++ with the OpenCV library, which is a library image-processing although that's not relevant for this question. Currently I have a design decision to make. OpenCV, being a C library, has its data structures (such as CvMat) declared as structs. To create them, you use functions like cvCreateMat, and to release them, you use f...