Copy constructors, de/constructors and reference counts

J

Jeremy Smith

I am writing a program where speed is crucial, and one optimisation is
that blocks of memory are re-used instead of being copied around and re-
allocate.

I have written my own vector-style class to achieve this. Here is what
happens to the reference count:

class MyArray:

Constructor:
Ref count is set to 1

Destructor:
Ref count --

Copy constructor (operator=):
Ref count ++

This works okay as long as MyArray is instantiated on its own. But when I
include this class as part of another struct or class, this is what
happens:

struct Region:
MyArray array;

Constructor:
Ref count of array ++

This transcript shows what happens (CharArray is the name of my array
class):

MyArray():Refcount:1 Pointer:0
Region()
MyArray():Refcount:1 Pointer:0
Region()
Allocate MyArray data = 4921216
Leaving
~Region()
~MyArray:Refcount:0 Pointer:4921216
Refcount = 0 so freeing 4921216
Region.operator=
MyArray operator=:Refcount:1 Pointer:0
~Region()
~MyArray:Refcount:0 Pointer:0
~Region()
~MyArray:Refcount:-1 Pointer:0

The region's copy constructor is called *after* the CharArray has been
destructed! Which seems odd. The reference count hits 0, and it frees up
the data, before the region's copy constructor has had a chance to
increase the reference count (calling MyArray's copy constructor will do
this anyway).

So my question is, why is the destructor called on the old Region
instance before the copy constructor called to copy over the data?

Any comments would be appreciated. I haven't posted the array class's
code because I need to tidy it up, but if anyone needs me to tidy it and
post it, I will.

Jeremy.
 
T

Thomas J. Gritzan

Jeremy said:
I am writing a program where speed is crucial, and one optimisation is
that blocks of memory are re-used instead of being copied around and re-
allocate.

Thats what references usually are for: Avoiding copies.
boost::shared_ptr could be usefull, too.

But the first question should be: _Why_ do you think that speed is
crucial for your programm?
I have written my own vector-style class to achieve this. Here is what
happens to the reference count:
[snipped problem description]

Thats clearly in the FAQ, 5.8:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
 
J

Jeremy Smith

I am writing a program where speed is crucial, and one optimisation is
that blocks of memory are re-used instead of being copied around and
re- allocate.

I have written my own vector-style class to achieve this. Here is what
happens to the reference count:

Sorry everyone, I found the answer. What various websites had told me was a
copy constructor, was in fact operator=. I replaced it with a copy
constructor and now it increments the reference count before calling the
destructor.

Cheers,

Jeremy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top