ref counted pointers going out of scope

Discussion in 'C++' started by Angus, Nov 19, 2008.

  1. Angus

    Angus Guest

    Hello

    If I have a function which creates a pointer like this:

    int number = 44; //global

    void MyFunction() {
    int* pNum = &number;
    }

    Then as soon as MyFunction returns then pNum goes out of scope and
    becomes no more.

    But I am looking at some code that does this:

    CRQueue::ptr_type pqueue = thisThread->GetQueue();

    The next step is that a timer is setup to periodically exercise some
    function using some items that have been added to the queue.

    As the pqueue is reference counted, I am assuming it doesn't go out of
    scope and so the queue will still be valid (presumably until all items
    in the queue have benn processed by the timer function?).


    I am really just looking for confirmation that this is possible to
    do. If so, I then need to read up on reference counted pointers. Any
    hints on good sources of info would be appreciated.
    Angus, Nov 19, 2008
    #1
    1. Advertising

  2. On Nov 19, 11:54 am, Angus <> wrote:

    []

    > But I am looking at some code that does this:
    >
    > CRQueue::ptr_type pqueue = thisThread->GetQueue();
    >
    > The next step is that a timer is setup to periodically exercise some
    > function using some items that have been added to the queue.
    >
    > As the pqueue is reference counted, I am assuming it doesn't go out of
    > scope and so the queue will still be valid (presumably until all items
    > in the queue have benn processed by the timer function?).


    It does go out of scope.

    > I am really just looking for confirmation that this is possible to
    > do.  If so, I then need to read up on reference counted pointers.  Any
    > hints on good sources of info would be appreciated.


    The idea behind reference counting is that there is a reference
    counter associated with an object (often it is simple a member of an
    object). When you create a smart-pointer to that object, the
    constructor of the pointer increments the counter. When the pointer
    gets destroyed, its destructor of the pointer decrements the reference
    counter. When the reference counter reaches 0 it means that there are
    no more pointers referencing that objects, so that the object can be
    destroyed.

    In your particular case, there is a (probably work) queue associated
    with a thread. The queue must be owned by the thread object, so no
    matter what kind of pointer you use to refer to that queue, the queue
    exists as long as the thread owning it exists.

    --
    Max
    Maxim Yegorushkin, Nov 19, 2008
    #2
    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. Matthias Kaeppler
    Replies:
    5
    Views:
    436
    Axter
    Sep 10, 2005
  2. Replies:
    0
    Views:
    324
  3. Replies:
    22
    Views:
    729
    peter koch
    Apr 30, 2008
  4. Replies:
    6
    Views:
    324
    James Kanze
    Apr 29, 2008
  5. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    131
    Navindra Umanee
    Feb 12, 2005
Loading...

Share This Page