Question about example using ArrayOfValues smart pointers

Ruslan Zasukhin sunshine at public.kherson.ua
Wed Oct 18 11:07:00 CDT 2006


On 18.10.2006 04:22, "Matthew Jew" <mjew at icnc.com> wrote:

Hi Matthew, 
 
> There is some sample code in VSDK_Examples that I have a question about.
> The code uses a smart pointer to an ArrayOfValues.
> If I understand smart pointers (and I know that I may NOT be
> understanding them correctly),
> when a smart pointer goes out of scope, it does a Release() on the
> object it points to.
> If the Release() causes the reference count to go to zero, the object
> is deallocated.
> 
> In the example, the procedure FillT1Table calls the function
> BuildArrayOfBinds, which is supposed
> to return an ArrayOfValues_Ptr.
> 
> At the start, BuildArrayOfBinds declares a ArrayOfValues_Ptr called
> pBinds and assigns a newly created ArrayOfValues to it.
> At this point, my understanding is that the Reference Count for the
> object would be 1.
> 
> At the conclusion of the function BuildArrayOfBinds, pBinds would
> then go out of scope,
> sending a Release() to the ArrayOfValues allocated at the start (and
> assigned to pBinds).
> This would decrement the Reference Count to 0, and deallocate the
> object.

Almost correct. But not right in this case.

** if you have function that return VOID then RIGHT

void BuildArrayOfBinds(
{
    ArrayOfValues_Ptr pBinds = new ArrayOfValues();
}

Pbinds smart pointer will be just destroyed on exit of function,
Its destructor is called, it calls Release, we get get counter 0.

But we have 

ArrayOfValues_Ptr BuildArrayOfBinds(
{
    ArrayOfValues_Ptr pBinds = new ArrayOfValues();

    return pBinds;
}

This make difference. Now C++ compiler on return create TMP object
Of ArrayOfValues_Ptr  type using copy constructor.

Copy ctor raise counter to 2.
After this is called destructor for pBinds and we get counter 1.
So object that smart_ptr keeps still is alive.

 
> Upon returning to FillT1Table, the location of the ArrayOfValues that
> the function created would be returned
> and assigned to the smart pointer ArrayOfValues_Ptr pBinds1, but at
> that point, isn't the object pointed to by
> this value already deallocated?
> 
> Wasn't there supposed to be an AddRef() somewhere in BuildArrayOfBinds,
> and a corresponding Release() somewhere in FillT1Table?
> 
> Or is there something else going on that I don't understand?
> What am I missing?

-- 
Best regards,

Ruslan Zasukhin
VP Engineering and New Technology
Paradigma Software, Inc

Valentina - Joining Worlds of Information
http://www.paradigmasoft.com

[I feel the need: the need for speed]




More information about the Valentina mailing list