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.
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.