473,499 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reference counting and API (const reference vs pointer oriented)

Hi there

I have implemented a very simple smartpointer class (invasive
design). And I was wondering what should be the natural API when using
those in a Container.

I choose to define the following operator in my smartpointer class:

....
operator ObjectType * () const
{ return Pointer; }
....

which in turn forces me to have a pointer interface (instead of const
reference):

class Container {
public:
Container():Instance(0) {}
void Set(Object *o) { Instance = o; } // Pointer interface
private:
SmartPointer<ObjectInstance;
};
I would have preferred a const reference interface, such as:

class Container {
public:
Container():Instance(0) {}
void Set(Object const &o) { Instance = const_cast<Object*>(&o); }
private:
SmartPointer<ObjectInstance;
};

Should I refactor my smartpointer class to be more reference oriented
instead of pointer oriented. I do not see a case where the smart
pointer should point to a NULL pointer.

Comments ?

thanks
-Mathieu
// Full code:
#include <assert.h>

template<class ObjectType>
class SmartPointer
{
public:
SmartPointer():Pointer(0) {}
SmartPointer(const SmartPointer<ObjectType>& p):Pointer(p.Pointer)
{ Register(); }
SmartPointer(ObjectType* p):Pointer(p)
{ Register(); }
~SmartPointer() {
UnRegister();
Pointer = 0;
}
ObjectType *operator -() const
{ return Pointer; }
operator ObjectType * () const
{ return Pointer; }
void operator = (SmartPointer const &r)
{ return operator = (r.GetPointer()); }
void operator = (ObjectType *r)
{
if(Pointer) Pointer->UnRegister();
Pointer = r;
if(Pointer) Pointer->Register();
}
private:
void Register() { if(Pointer) Pointer->Register(); }
void UnRegister() { if(Pointer) Pointer->UnRegister(); }

ObjectType* Pointer;
};

class Object
{
public:
Object():ReferenceCount(0) {}
virtual ~Object() { assert(ReferenceCount == 0); }

void Register() {
ReferenceCount++;
}
void UnRegister() {
ReferenceCount--;
if(!ReferenceCount) delete this;
}
private:
long ReferenceCount;
};

class Foo : public Object
{
public:
int F;
};

class Container {
public:
Container():Instance(0) {}
void Set(Object *o) { Instance = o; }
private:
SmartPointer<ObjectInstance;
};

int main()
{
SmartPointer<Fooo = new Foo;
Container c;
c.Set( o );
return 0;
}
Aug 28 '08 #1
8 1836
mathieu wrote:
class Object
{
public:
Object():ReferenceCount(0) {}
virtual ~Object() { assert(ReferenceCount == 0); }

void Register() {
ReferenceCount++;
}
void UnRegister() {
ReferenceCount--;
if(!ReferenceCount) delete this;
}
private:
long ReferenceCount;
};

class Foo : public Object
{
public:
int F;
};
You have a basic problem with your reference-counting base class there.

Assume you have one instance of Foo allocated behind one SmartPointer
(let's name it fooPtr1). The reference count of that Foo instance will be 1.
Also assume you have another instance of Foo allocated and shared
between two SmartPointers (let's name them fooPtr2 and fooPtr3). The
reference count of this instance will be 2.

Now assume you do a "*fooPtr1 = *fooPtr2;", which is not in any way a
far-fetched thing to do. What happens?

What happens is that now the object behind fooPtr1 will have its
reference counter set to 2, even though only one smart pointer is
pointing to it. When fooPtr1 is destroyed, it will decrement that
counter to 1, not destroy the object, and you have a memory leak.

Assume the inverse situation: "*fooPtr2 = *fooPtr1;" What happens?

What happens is that now the second object will have its reference
counter set to 1 even though two smart pointers are pointing to it. When
one of then is destroyed, the other will point to freed memory. Kaboom.

The solution to this problem is rather simple, though.
Aug 28 '08 #2
On Aug 28, 9:56*pm, Juha Nieminen <nos...@thanks.invalidwrote:
mathieu wrote:
class Object
{
public:
* Object():ReferenceCount(0) {}
* virtual ~Object() { assert(ReferenceCount == 0); }
* void Register() {
* * ReferenceCount++;
* }
* void UnRegister() {
* * ReferenceCount--;
* * if(!ReferenceCount) delete this;
* * }
private:
* long ReferenceCount;
};
class Foo : public Object
{
public:
* int F;
};

* You have a basic problem with your reference-counting base class there.

* Assume you have one instance of Foo allocated behind one SmartPointer
(let's name it fooPtr1). The reference count of that Foo instance will be1.
* Also assume you have another instance of Foo allocated and shared
between two SmartPointers (let's name them fooPtr2 and fooPtr3). The
reference count of this instance will be 2.

* Now assume you do a "*fooPtr1 = *fooPtr2;", which is not in any waya
far-fetched thing to do. What happens?

* What happens is that now the object behind fooPtr1 will have its
reference counter set to 2, even though only one smart pointer is
pointing to it. When fooPtr1 is destroyed, it will decrement that
counter to 1, not destroy the object, and you have a memory leak.

* Assume the inverse situation: "*fooPtr2 = *fooPtr1;" What happens?

* What happens is that now the second object will have its reference
counter set to 1 even though two smart pointers are pointing to it. When
one of then is destroyed, the other will point to freed memory. Kaboom.

* The solution to this problem is rather simple, though.
Move the cstor/dstor of Object to the protected: section ?
I realized that only now, but that does not work for derived class.

Any other suggestion I might be missing ?

Thanks
-Mathieu
Aug 28 '08 #3
mathieu wrote:
Any other suggestion I might be missing ?
Yes: Implement a copy constructor and assignment operator in your
reference-counting base class which do *not* copy the reference counter
member variable. (If there's nothing else in the base class, then simply
make empty implementations of them.)

This way you can freely copy/assign derived objects without messing up
the reference counters.
Aug 28 '08 #4
On Aug 29, 12:39 am, Juha Nieminen <nos...@thanks.invalidwrote:
mathieu wrote:
Any other suggestion I might be missing ?

Yes: Implement a copy constructor and assignment operator in your
reference-counting base class which do *not* copy the reference counter
member variable. (If there's nothing else in the base class, then simply
make empty implementations of them.)

This way you can freely copy/assign derived objects without messing up
the reference counters.
It indeed solve the issue you reported, but does not solve the issue
where one would do:

Foo f = *fooPtr3;

Thanks again
-Mathieu
Aug 29 '08 #5
mathieu wrote:
It indeed solve the issue you reported, but does not solve the issue
where one would do:

Foo f = *fooPtr3;
I don't see what's wrong with that.
Aug 29 '08 #6
On Aug 29, 4:45*pm, Juha Nieminen <nos...@thanks.invalidwrote:
mathieu wrote:
It indeed solve the issue you reported, but does not solve the issue
where one would do:
*Foo f = *fooPtr3;

* I don't see what's wrong with that.
<quote>
Yes: Implement a copy constructor and assignment operator in your
reference-counting base class which do *not* copy the reference
counter
member variable. (If there's nothing else in the base class, then
simply
make empty implementations of them.)
</quote>

Which lead to:

Object(const Object&){ } // see how ReferenceCount is *never*
initialized
void operator=(const Object&){ }

Thus when you do:

Foo f = *fooPtr3;
// f.ReferenceCount is pretty much random memory

thanks
-Mathieu
Aug 31 '08 #7
On Aug 31, 10:28*am, mathieu <mathieu.malate...@gmail.comwrote:
On Aug 29, 4:45*pm, Juha Nieminen <nos...@thanks.invalidwrote:
mathieu wrote:
It indeed solve the issue you reported, but does not solve the issue
where one would do:
*Foo f = *fooPtr3;
* I don't see what's wrong with that.

<quote>
Yes: Implement a copy constructor and assignment operator in your
reference-counting base class which do *not* copy the reference
counter
member variable. (If there's nothing else in the base class, then
simply
make empty implementations of them.)
</quote>

Which lead to:

* Object(const Object&){ *} // see how ReferenceCount is *never*
initialized
* void operator=(const Object&){ *}

Thus when you do:

* Foo f = *fooPtr3;
* // *f.ReferenceCount is pretty much random memory

thanks
-Mathieu
Alright found the answer in:

http://www.parashift.com/c++-faq-lit...html#faq-16.24

And copy cstor need to initialize the ref count:

Object(const Object&):ReferenceCount(0){}
void operator=(const Object&){ }

Thanks the ref counting is much user friendly !

-Mathieu
Aug 31 '08 #8
mathieu wrote:
<quote>
Yes: Implement a copy constructor and assignment operator in your
reference-counting base class which do *not* copy the reference
counter
member variable.
I said you should not copy the reference counter. I didn't say you
shouldn't initialize it.
Aug 31 '08 #9

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

Similar topics

15
1800
by: David Fisher | last post by:
I have been encouraged by someone else to use a reference rather than a pointer as an "out" parameter to a function, eg. void doStuff(Thing &out1, Thing &out2) as opposed to: void...
5
3725
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
1
3226
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
10
2250
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
4
4174
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
20
1633
by: Protoman | last post by:
OK, this code compiles, links, and executes, but, how do I setup, like, a spinlock to query the DataBase object's status to let the SmrtPtr know that the object's been deleted?: #pragma once ...
8
9122
by: Bart Simpson | last post by:
If a class has a member variable that is a reference. What happens to teh class that is being referenced, when the containing class is destroyed? e.g. Class A{ }; Class B { B(const A&...
1
1478
by: PeterAPIIT | last post by:
What is template template arguments and template typename arguments ? The reason i write in policy based design is because this is the requirement of the assignment. My code so far: ...
275
12026
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7132
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
7009
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
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
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,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.