smart-pointers

Should a library use an interface that uses smart pointers?

Hi, I'm starting to write a library and considering its interface. Previous libraries I've written all use raw pointers (both internally and in its interface), and now I want to try the smart pointer library that comes with VS2010. Should the interface use smart pointers? (Possibly forcing the library users to use smart pointers too?) ...

Simple reference counting: smart pointers

I would like to implement a simple reference counting using smart pointers. The variable pointer represents pointer to stored object, reference_count represents total count of copies of the object. if we initialize an object using NULL: reference_count = -1 else reference_count = 1 copy ctor and operator = increment reference_count des...

how to return an null tr1::shared_ptr and test if it is null

Hi, I have a function getA() with following signature : class A { public: typedef std::tr1::shared_ptr <A> Ptr; //other member functions.... }; class B { pubic: A::Ptr getA(); }; And, I want to return an empty pointer in getA() in same case; Also, as a user of Class B , I need to test if the return value of getA() is null bef...

Smart pointers, or "better" destructor

Which type of the smart pointer (shared, scoped) would be for such a data structures the most suitable... Structure 1: //Class with cross-references to points p1, p2 class PointTopo { private: double x, y; PointTopo * p1; PointTopo * p2; public: PointTopo(double xx, double yy): x(xx), y(yy) {this-> p1 = NULL; this->p...

Smart Pointer Implementation in C

Possible Duplicate: Smart pointers/safe memory management for C? I have an embedded application where I am allocating an object in dynamic memory and passing it around to other modules. I would like to create a smart pointer to this object. There are many examples in C++ for using and implementing smart pointers. I am l...

Shared pointers: pointer to pointer

Common pointers allows you to create pointer to pointer: void foo(Object **o) {} int main() { Object * o = new Object(); foo(&o); } Is there an analogous construction for shared_ptr? void foo(std::shared_ptr <Object> *o) {} int main() { std::shared_ptr <Object> o(new Object()); foo(&o); } ...

can scoped_ptr and shared_ptr be mixed?

Hi! Imagine I create an instance of Foo on the heap in a method/function and pass it to the caller. What kind of smartpointer would I use? smartptr new_foo() { smartptr foo = new Foo(); return foo; } void bar() { smartptr foo = new_foo(); foo->do_something(); // now autodelete foo, don't need it anymore } Ok... n...

tr1::unique_ptr and SelectObject()

Hi, I have some original code that manages exception safety like this: void foo() { HDC hdc = //get an HDC HBITMAP hbitmap = //get an HBITMAP HGDIOBJ hbitmapOld = SelectObject(hdc, hbitmap); try { //do something that may throw an exception } catch (...) { SelectObject(hdc, hbitmapOld); thro...

RAII in C++/CLI

I'm used to the C++ RAII facilities, and I want to use RAII the right way with managed code in C++/CLI. Herb Sutter and Microsoft both tell me this is the best practice. I have something like this: ref struct Managed { // No default constructor Managed( /*...*/ ) { /*...*/ } ~Managed() { /* Important non-managed resource re...

Is there a general smart pointer like auto_ptr and shared_ptr that doesn't need C++0x?

I'm wanting a non-reference counted smart pointer that can combine some of the useful aspects of auto_ptr and shared_ptr. I think that C++0x's unique_ptr is ultimately what I'd need, but I need something that will compile on Visual Studio 2008 and Xcode (gcc 4.2). The functionality I need is: Usable in factory methods so that owners...

Returning a unique_ptr from a class method C++0x

If my class SomeType has a method that returns a element from the map (using the key) say std::unique_ptr<OtherType> get_othertype(std::string name) { return otMap.find(name); } that would enure the caller would recieve a pointer to the one in the map rather than a copy? Is it ok to do this, or would it try and call the copy const...

C++ FAQ Lite Smart_Ptr Class Not Functioning?

I'm currently doing a lot of things on exception safety. (Herb Sutter's Exceptional C++, C++ FAQ Lite, etc) In particular, I wanted to write and understand the reference counting example of C++ FAQ Lite, but I'm currently stuck on this part of the code: class Smart_Ptr; class Foo { private: friend class Smart_Ptr; unsigned in...

Boost Smart Pointers and threading

If you have to pass objects across threads which smart pointer type is best to use? Assuming the object being passed is thread safe. ...

Do boost::shared_ptr<T> and boost::shared_ptr<const T> share the reference count?

There are several interesting questions on pitfalls with boost::shared_ptrs. In one of them, there is the useful tip to avoid pointing boost::shared_ptr<Base> and boost::shared_ptr<Derived> to the same object of type Derived since they use different reference counts and might destroy the object prematurely. My question: Is it safe to ha...

Getting started with smart pointers in C++

I have a C++ application which makes extensively use of pointers to maintain quite complex data structures. The application performs mathematical simulations on huge data sets (which could take several GB of memory), and is compiled using Microsoft's Visual Studio 2010. I am now reworking an important part of the application. To reduc...

shared_ptr magic :)

Mr. Lidström and me had an argument :) Mr. Lidström's claim is that a construct shared_ptr<Base> p(new Derived); doesn't require Base to have a virtual destructor. @Daniel: Really? Will the shared_ptr clean up correctly? Could you please in this case demonstrate how that effect could be implemented? – Armen Tsirunyan @Armen: The sh...

Reference count when returning a shared_ptr

Does the following code mean that when this function returns, the request object inside this class still holds a reference to this object? boost::shared_ptr<Request> RequestList::GetRequest() { boost::mutex::scoped_lock(listmtx); request = boost::shared_ptr<Request>(new Request()); return request; } used: request = reques...

auto_ptr with swig

Hi, I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers here[0]. But I can't get it to work. swig generates code that wants to initialize the auto_ptr using a const reference, but auto_ptr defines the cop...

C++ shared_ptr how to delete a pointee with multiple shared_ptr

Im working on this project, The problem Im having is that the an object, does not really get deleted when I need it to be because it has a couple of shared pointers pointing to it. How do I solve this,please help. ...

How do non-intrusive smart pointers behave with respect to inheritance and multiple inheritance?

I am using C++. C++0x using Visual Studio 2010 to be correct. Suppose I have a class Z. To make it safer in my application to work with pointers to this class, I can consistently use smart pointers (shared pointer, weak pointer). Now this class Z inherits from a class X. Some parts of my application will work with pointers to class ...