Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy constructors, de/constructors and reference counts

Reply
Thread Tools

Copy constructors, de/constructors and reference counts

 
 
Jeremy Smith
Guest
Posts: n/a
 
      08-02-2006
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.
 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      08-02-2006
Jeremy Smith schrieb:
> 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-lit...t.html#faq-5.8

--
Thomas
 
Reply With Quote
 
 
 
 
Jeremy Smith
Guest
Posts: n/a
 
      08-02-2006
Jeremy Smith <(E-Mail Removed)> wrote in
news:Xns98141C612C36jeremyalansmithsofth@216.196.1 09.145:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread-safe reference counts. jason.cipriani@gmail.com C++ 117 04-10-2008 09:45 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
Debugging reference counts in python/C karmadharma@gmail.com Python 0 08-30-2006 09:32 PM
Reference Counts raghu Python 5 05-19-2006 03:59 PM
If...Else and variables and counts Tank Javascript 1 02-11-2004 02:50 PM



Advertisments