Boost logo

Boost :

From: Robert Bell (belvis_at_[hidden])
Date: 2004-03-08 22:32:53


Pavel Vozenilek wrote:
> "David Abrahams" <dave_at_[hidden]> wrote
>
>
>>I can't understand this claim. If the counter works properly, then it
>>will stay valid.
>>
>
> This example was too simplified (and thus not to the point).
>
> More complex one:
>
> -----------------------
> struct my_class
> {
> void* database_handle;
>
> my_class() { database_handle = allocate_handle(); }
> ~my_class() { destroy_db_handle(database_handle); }
> my_class& operator=(const my_class& other) {
> // no copy of database handle, not needed,
> // not useful here
> }
> my_class(const my_class& other) {
> database_handle = allocate_handle(); // needs unique db connection
> }
> };
>
> circular_buffer<my_class> buffer(1);
>
> {
> my_class a;
> buffer.push_back(a); // 2 handles after function returns
> ...
> }
>
> {
> my_class b;
> buffer.push_back(b); // desctructor not called, handle lost
> ...
> }

I don't see why a handle is lost.

{
// Only one handle in existence at this point, the one
// previously added to buffer by "buffer.push_back(a);".

    my_class b;

// Now two handles exist, one in b, and one in buffer.

    buffer.push_back(b);

// Two handles still exist -- push_back uses assignment, which
// doesn't affect handles at all. No new handles have been
// created by push_back(b).

}

// Now only one handle exists, in buffer, since b has
// gone out of scope and has been destroyed.

What am I missing?

Bob


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk