473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Detaching primitive from boost::shared_ptr?

I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.

Is there a way to do this?

Aug 2 '05 #1
6 3558
Sean wrote:
I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.

Is there a way to do this?

Copy the shared_ptr to a local shared_ptr that has the same scope
as the raw pointer.

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Aug 2 '05 #2
In message <11**********************@g49g2000cwa.googlegroups .com>, Sean
<sj*********@yahoo.com> writes
I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.

Is there a way to do this?


Why would you want to? The whole point of shared pointers is to avoid
this kind of unsafe manipulation of raw pointers.

If you want to share the pointed-to object, just take a copy of the
shared_ptr in another shared_ptr. That's what the "shared" in the name
means. For as long as there's at least one shared_ptr pointing to it,
the object won't be deleted - *provided* you don't do unsafe tricks with
raw pointers to the same object.

--
Richard Herring
Aug 3 '05 #3
* Sean:
I have a boost::shared_ptr and want to detach the primitive pointer
within it so I can keep using it even after the shared_ptr is
destroyed.
That is not a good idea; can you see why?

Is there a way to do this?


Yes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 3 '05 #4
Agreed. It's not a good idea. However, I still need to do it. Can you
post sample code on how to do it?

thank you.

Aug 4 '05 #5

"Sean" <sj*********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Agreed. It's not a good idea. However, I still need to do it. Can you
post sample code on how to do it?


Can you post sample code on why you think you need to do it?

Thanks, Jeff
Aug 4 '05 #6
* Sean:
Agreed. It's not a good idea. However, I still need to do it. Can you
post sample code on how to do it?


Note first that normally you'd just use another shared_ptr to keep a
reference to the original's referred object.

And when that isn't feasible, you'd normally use boost::weak_ptr.

I'm therefore almost certain that what you're trying to do is Very Bad
Design (TM), that you could very easily solve the problem by improving the
design, but if you absolutely want to live extremely dangerously, e.g.

#include <iostream>
#include <ostream>
#include <boost/shared_ptr.hpp>

class X
{
public:
typedef boost::shared_ptr<X> AutoPtr;
static AutoPtr instance(){ return AutoPtr( new X, &destroy ); }
~X(){ std::cout << "Destroyed" << std::endl; }
X* extendedLifetime() { myExtendedLife = true; return this; }
private:
bool myExtendedLife;
X(): myExtendedLife( false ) { std::cout << "Created" << std::endl; }
static void destroy( X* p ) { if( !p->myExtendedLife ){ delete p; } }
};

void doStuff( X::AutoPtr ) {} // Whatever.

X* aUsedNewX()
{
X::AutoPtr p( X::instance() );
doStuff( p );
return p->extendedLifetime();
}

int main()
{
// C-oriented calling code.
X* p = aUsedNewX();
std::cout << "main()" << std::endl;
delete p;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 4 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented...
5
by: ctick | last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?
6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
1
by: rssmps | last post by:
Hi, first post and here it goes... This is the current code (working) that I have. It works but for large number of A & B objects, it's not the most efficient. It's also not really heterogenous...
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.