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

RefCounts

Could someone illustrate how I could implement reference counting within my
base class which all my classes are derived from so that all my objects are
reference counted. This way once I create my classes and all their
references have been released, the object is then deleted instead of me
having to keep up with whether it has 0 references or not?

Thanks in advance!
Chris

--

Jul 22 '05 #1
5 1134
"news.tkdsoftware.com" <ch***@noreply.nospam.tkdsoftware.com> wrote in
message news:wc****************************@tkdsoftware.co m...
Could someone illustrate how I could implement reference counting within my base class which all my classes are derived from so that all my objects are reference counted. This way once I create my classes and all their
references have been released, the object is then deleted instead of me
having to keep up with whether it has 0 references or not?


The FAQ (http://www.parashift.com/c++-faq-lite/) contains a good example of
a reference-counted pointer implementation under section 16 ("Freestore
management") questions 21 ("How do I do simple reference counting?") on. If
you inherit from the class that the FAQ calls Fred, you should be able to
use "FredPtr" for reference counting instances of the derived class, too.
Don't forget the virtual destructor, of course. Also, instead of writing
your reference counting own code, consider using something like
boost::shared_ptr.

--
David Hilsee
Jul 22 '05 #2

"David Hilsee" <da*************@yahoo.com> wrote in message:
Don't forget the virtual destructor, of course. Also, instead of writing
your reference counting own code, consider using something like
boost::shared_ptr.


Good advice. You can also reuse boost::detail::shared_count, which is what I
happen to be doing right now ;-)

Jonathan
Jul 22 '05 #3
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:2s*************@uni-berlin.de...

"David Hilsee" <da*************@yahoo.com> wrote in message:
Don't forget the virtual destructor, of course. Also, instead of writing your reference counting own code, consider using something like
boost::shared_ptr.
Good advice. You can also reuse boost::detail::shared_count, which is what

I happen to be doing right now ;-)


Out of curiosity, why did you choose to use shared_count and not shared_ptr?

--
David Hilsee
Jul 22 '05 #4

"David Hilsee" <da*************@yahoo.com> wrote in message
news:--********************@comcast.com...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:2s*************@uni-berlin.de...

"David Hilsee" <da*************@yahoo.com> wrote in message:
Don't forget the virtual destructor, of course. Also, instead of writing your reference counting own code, consider using something like
boost::shared_ptr.


Good advice. You can also reuse boost::detail::shared_count, which is what

I
happen to be doing right now ;-)


Out of curiosity, why did you choose to use shared_count and not shared_ptr?


I'm writing a reference-counted shared pointer with slightly diferent semantics
than shared_ptr. It's part of an aspect-oriented programming library I'm working
on with Chrisopher Diggins. We're planning a preliminary announcement soon.

Jonathan
Jul 22 '05 #5
news.tkdsoftware.com wrote:
Could someone illustrate how I could implement reference counting within my
base class which all my classes are derived from so that all my objects are
reference counted. This way once I create my classes and all their
references have been released, the object is then deleted instead of me
having to keep up with whether it has 0 references or not?


I've just looked at the FAQ example, and it looks neat.

I did something similar a while ago to implement reference-counted
vectors. The two classes were called Vector and VectorData. The idea
was that the class user need only concern him/herself with Vector, as it
wrapped the various VectorData methods to provide functionality.

Don't forget that reference counting won't work if there's any chance of
circular references in your data structure.

Stewart.
Jul 22 '05 #6

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

Similar topics

4
by: J-P | last post by:
Hi, I have a Python script interacting with a specialized C numerical library. The whole program does quite a lot of number crunching as should be running for a couple of hours. However, it...
12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
0
by: Jean-François Doyon | last post by:
Hello, I'm working with Zope 2.7 and Python 2.3.3, and I have some leaks I'm trying to track down. So, more generally, I'm wondering what might be good techniques to track down where...
9
by: David Turner | last post by:
Further to the discussion from earlier titled "does python have useless destructors?", I've posted a brief summary of garbage collection, RAII, and the "using" clause (which is what PEP310's...
20
by: Ken Godee | last post by:
module1 calls a function in module2 module2 starts a thread that calls a function in module3 and then returns to module1 thread finishes and I need the return value from the thread to use in...
7
by: Mike Thompson | last post by:
The interning of strings has me puzzled. Its seems to happen sometimes, but not others. I can't decern the pattern and I can't seem to find documentation regarding it. I can find documentation...
62
by: robert | last post by:
I'd like to use multiple CPU cores for selected time consuming Python computations (incl. numpy/scipy) in a frictionless manner. Interprocess communication is tedious and out of question, so I...
9
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.