473,379 Members | 1,326 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,379 software developers and data experts.

Smart pointer for observe only behavior

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
Jul 10 '08 #1
5 2171
On Jul 10, 4:26*pm, Michael DOUBEZ <michael.dou...@free.frwrote:
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
Jul 10 '08 #2
Soumen <so******@gmail.comwrote:
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.

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.
Jul 11 '08 #3
On Jul 11, 7:40*am, "Daniel T." <danie...@earthlink.netwrote:
Soumen <soume...@gmail.comwrote:
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.
Jul 11 '08 #4
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.
Jul 11 '08 #5
Soumen <so******@gmail.comwrote:
"Daniel T." <danie...@earthlink.netwrote:
Soumen <soume...@gmail.comwrote:
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?
Think of a Pointer class that has a "kill" method. When any owner of a
Pointer object calls kill, the object is deleted and all the Pointers
that point to the same object are set to NULL.
Is it not shared_ptr and weak_ptr combo type soln?
I don't know.
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.
In that case, keep a pointer to the last instance of A that you used,
call A::instance() again and compare the pointers to see if they are the
same...
Jul 12 '08 #6

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

Similar topics

4
by: gorda | last post by:
Hello, I have the following code, with a basic class having a constructor and destructor. in the main section, I create a static pointer to the class. My question is regarding the destructor. If...
3
by: Vijai Kalyan | last post by:
I have been thinking about this and it may have already been thrashed out and hung out to dry as a topic of no more interest but here goes. I found when implementing a smart pointer that the...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
14
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun...
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.