Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Smart pointer for observe only behavior

Reply
Thread Tools

Smart pointer for observe only behavior

 
 
Soumen
Guest
Posts: n/a
 
      07-10-2008
Hi,

I've requirement to observe a raw pointer (i.e. I should be able to
query if the pointer I'm using is still valid or not but when the
observer goes out of scope, the resource -- memory -- shouldn't be
released). Is there any boost way (or using any other smart pointer)
to achieve this? Since I've a raw pointer, probably I cannot use
weak_ptr.

Is there a way to do this using shared_ptr with custom deleter?

Regards,
~ Soumen
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      07-10-2008
Soumen a écrit :
> I've requirement to observe a raw pointer (i.e. I should be able to
> query if the pointer I'm using is still valid or not but when the
> observer goes out of scope, the resource -- memory -- shouldn't be
> released). Is there any boost way (or using any other smart pointer)
> to achieve this? Since I've a raw pointer, probably I cannot use
> weak_ptr.



Your can only ask if a pointer is valid to the owner of this pointer. If
your pointer is not owned then you have no way to know if it is still
valid.

> Is there a way to do this using shared_ptr with custom deleter?


In the case of shared_ptr/weak_ptr, it is the shared_ptr that owns the
pointer and it is what makes weak_ptr possible. Using a shared_ptr
without destructor won't save you.

--
Michael
 
Reply With Quote
 
 
 
 
Soumen
Guest
Posts: n/a
 
      07-10-2008
On Jul 10, 4:26*pm, Michael DOUBEZ <michael.dou...@free.fr> wrote:
> Soumen a écrit :
>
> > I've requirement to observe a raw pointer (i.e. I should be able to
> > query if the pointer I'm using is still valid or not but when the
> > observer goes out of scope, the resource -- memory -- shouldn't be
> > released). Is there any boost way (or using any other smart pointer)
> > to achieve this? Since I've a raw pointer, probably I cannot use
> > weak_ptr.

>
> Your can only ask if a pointer is valid to the owner of this pointer. If
> your pointer is not owned then you have no way to know if it is still
> valid.
>
> > Is there a way to do this using shared_ptr with custom deleter?

>
> In the case of shared_ptr/weak_ptr, it is the shared_ptr that owns the
> pointer and it is what makes weak_ptr possible. Using a shared_ptr
> without destructor won't save you.
>
> --
> Michael


OK. The pointer I want to observe say is of class A and class A is
singleton.
the creation and destruction of A not managed by different module
(legacy).
But my module (new) needs to use some member functions of A. My module
cannot
manage A but is dependent on A. During course of execution, there're
chances that
A's object is recreated. And I need to be aware of it. Is there a
solution?

I can at most change A::instance() to return shared_ptr<A>.

Regards,
~ Soumen
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      07-10-2008
Soumen a écrit :

> OK. The pointer I want to observe say is of class A and class A is
> singleton.


I assume you mean a global.

> the creation and destruction of A not managed by different module
> (legacy).


I don't understand. You mean the module don't care that A can be replaced ?

> But my module (new) needs to use some member functions of A. My module
> cannot
> manage A but is dependent on A. During course of execution, there're
> chances that
> A's object is recreated. And I need to be aware of it. Is there a
> solution?


I assume you are un multithreaded environment, otherwise, there is no
problem (just check the pointer you have is the same as the singleton's).

> I can at most change A::instance() to return shared_ptr<A>.


If you can do it, it seems as good solution but hen, I don't know your
problem space.

An alternative (perhaps more clumsy) is to provide an observer pattern
that calls you if the singleton is replaced and then you can act
accordingly.

--
Michael

 
Reply With Quote
 
Soumen
Guest
Posts: n/a
 
      07-11-2008
On Jul 11, 7:40*am, "Daniel T." <danie...@earthlink.net> wrote:
> Soumen <soume...@gmail.com> wrote:
> > I've requirement to observe a raw pointer (i.e. I should be able to
> > query if the pointer I'm using is still valid or not but when the
> > observer goes out of scope, the resource -- memory -- shouldn't be
> > released). Is there any boost way (or using any other smart
> > pointer) to achieve this? Since I've a raw pointer, probably I
> > cannot use weak_ptr.

>
> > Is there a way to do this using shared_ptr with custom deleter?

>
> No. You would have to write some sort of observable pointer. I've done
> this before but be advised that such a pointer is quite smart and kind
> of expensive. You would need two classes, a "master_pointer" that owns
> the object in question and "observer_pointers". observer_pointers log
> themselves with the master_pointer as holders of pointers to the object
> and the master_pointer notifies all the observers when the object is
> deleted so they will know the object is no longer valid.


Could you please elaborate a bit more? Is it not shared_ptr and
weak_ptr
combo type soln?

> I expect there is a better way to solve your problem though...
>
> > OK. The pointer I want to observe say is of class A and class A is
> > singleton. the creation and destruction of A not managed by
> > different module (legacy). But my module (new) needs to use some
> > member functions of A. My module cannot manage A but is dependent
> > on A. During course of execution, there're chances that A's object
> > is recreated. And I need to be aware of it. Is there a solution?

>
> Yes and the solution is simple. Don't keep any pointers to the A
> singleton. Every time you need to call a member-function on the
> singleton object, get the pointer from the A::instance() member-function.


Not quite clear as in my problem domain I need to know if A's object
is
recreated or not. If recreated, I need to do something, else not.
Observer
is probably a better soln. I'd though of it. But I's exploring there's
any
alternate quick soln to my problem rather than spending time to
implement
observer.

Anyway, thanks for all your inputs.
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      07-11-2008
Soumen wrote:

> Not quite clear as in my problem domain I need to know if A's object
> is
> recreated or not. If recreated, I need to do something, else not.
> Observer
> is probably a better soln. I'd though of it. But I's exploring there's
> any
> alternate quick soln to my problem rather than spending time to
> implement
> observer.
>
> Anyway, thanks for all your inputs.


An object can implement both the singleton and observer pattern.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Observe variable set and variable change saladin.mundi@gmx.de Ruby 6 11-19-2007 02:33 PM
safari Event.observe problem tompra Javascript 0 10-02-2006 06:26 PM
Observe a Java Program during runtime A. Meyer Java 1 09-21-2006 01:31 AM
prototype/ajax really simple Event.observe question Doug Lerner Javascript 0 04-16-2006 11:16 AM
How to observe the compution result? Peng Yu C++ 1 09-25-2004 10:34 PM



Advertisments