Copy constructors, de/constructors and reference counts

Discussion in 'C++' started by Jeremy Smith, Aug 3, 2006.

  1. Jeremy Smith

    Jeremy Smith Guest

    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.
    Jeremy Smith, Aug 3, 2006
    #1
    1. Advertising

  2. 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-lite/how-to-post.html#faq-5.8

    --
    Thomas
    Thomas J. Gritzan, Aug 3, 2006
    #2
    1. Advertising

  3. Jeremy Smith

    Jeremy Smith Guest

    Jeremy Smith <> wrote in
    news:Xns98141C612C36jeremyalansmithsofth@216.196.109.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.
    Jeremy Smith, Aug 3, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. raghu

    Reference Counts

    raghu, May 18, 2006, in forum: Python
    Replies:
    5
    Views:
    452
    Tim Peters
    May 19, 2006
  2. Replies:
    0
    Views:
    353
  3. Jess
    Replies:
    5
    Views:
    596
    Ron Natalie
    Jun 7, 2007
  4. Replies:
    117
    Views:
    3,894
    Thomas J. Gritzan
    Apr 10, 2008
  5. srp113
    Replies:
    3
    Views:
    465
Loading...

Share This Page