Hi Aaron!

Thanks again.

On Mon, Aug 3, 2015 at 4:30 PM, Aaron Laws <dartm...@gmail.com> wrote:
> On Mon, Aug 3, 2015 at 3:40 PM, K. Frank <kfrank2...@gmail.com> wrote:
>>
>> Hello List!
>>
>> Another question, again looking at the Wt::Dbo tutorial:
>>
>>    http://www.webtoolkit.eu/wt/doc/tutorial/dbo.html
>> ...
> ...
>> I have a question about exception safety:  What if the call to
>> session.add() throws?  My understanding is that userPtr will
>> not yet have been assigned, and will therefore not be able to
>> delete (the object.  Who then would have responsibility for calling
>> 'delete user;"?
>>
>> Is this issue just a simplification in the sample code and the (only?)
>> exception-safe way to do this would be:
>>
>>    {
>>       User *user = new User();
>>
>>       // this stuff can't throw -- true?
>>       user->name = "Joe";
>>       // ...
>>       dbo::ptr<User> userPtr((user);
>>
>>       // this could throw, but userPtr ensures that user will be deleted
>>       session.add(userPtr);
>>    }
>>
>> Is there ever any fully exception-safe use of the original version?
>>
>>    dbo::ptr<User> userPtr = session.add(&user);

(Oops ...  Typo on my part.  I meant to write "session.add(user)", where
user is of type User*.)

> Excellent question. Based on the current source, it should be okay to pass a
> pointer to a business object even in an exceptional situation. The source
> is:
>
> 228 template <class C>
> 229 ptr<C> Session::add(C *obj)
> 230 {
> 231   ptr<C> result(obj);
> 232   return add(result);
> 233 }
>
> If Wt::Dbo::Session::add(Wt::Dbo::ptr<C>&) throws, the original object will
> be deleted.

Ah, okay.  The overload that takes a raw pointer, Session::add (C*), creates
a Wt::Dbo::ptr smart pointer owning the C* before it does anything that might
throw, so were safe.

Or to say it another way, both:

   Wt::Dbo::ptr<User> userPtr (user);

and:

   Wt::Dbo::Session::add (user);

take ownership of (the object pointed to by the raw pointer) user, and become
responsible for deleting user, even if an exception is thrown.

> Of course, I can't think of many ways that session.add(&user);
> could be used, excepting something like

(Yes, of course.  The use of "&user" in this case was a typo on my part.)

> User * ptr_user = new User();
> User & user {*ptr_user};
> session.add(&user);
> /*We don't own ptr_user anymore, so don't delete it!*/
>
> Happy coding!
>
> In Christ,
> Aaron Laws


Best regards.


K. Frank

------------------------------------------------------------------------------
_______________________________________________
witty-interest mailing list
witty-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to