List:MySQL++« Previous MessageNext Message »
From:Jonathan Wakely Date:October 15 2009 11:15am
Subject:Re: mysqlpp::Connection copy c'tor segfaults
View as plain text  
2009/10/15 Warren Young:
> On Oct 14, 2009, at 9:24 PM, Adam Nielsen wrote:
>
>> I'm trying to open a new connection to a MySQL database by duplicating an
>> existing connection (as the docs[1] seem to indicate this is an acceptable
>> method) however it causes my application to segfault.
>
> That's not the purpose of the copy ctor.  It exists solely so you can say
> things like this:
>
>    void foo() {
>       Connection conn(...parms...);
>       ...do stuff...
>       return conn;
>    }
>
> Without a dedicated copy ctor to manage certain under-the-hood details, this
> would fail.

Eurgh.  Why does that work, but not the OP's usage?  Are you relying
on the fact that the original object is destroyed, so only one
Connection exists by the time it is used, even though two (or more)
can exist fleetingly?  That seems very fragile if so.

If copying is not safe, it shouldn't be allowed. The above example
could be replaced with:

std::auto_ptr<Connection> foo() {
  std::auto_ptr<Connection> conn(new Connection(...));
  ...
  return conn;
}

Or Connection could hold its internals by ref-counted ptr and copies
of the same object share their internals.
Thread
mysqlpp::Connection copy c'tor segfaultsAdam Nielsen15 Oct
  • Re: mysqlpp::Connection copy c'tor segfaultsWarren Young15 Oct
    • Re: mysqlpp::Connection copy c'tor segfaultsJonathan Wakely15 Oct
      • Re: mysqlpp::Connection copy c'tor segfaultsWarren Young15 Oct
        • Re: mysqlpp::Connection copy c'tor segfaultsJonathan Wakely15 Oct