[xsd-users] use of auto_ptr

Guy Kroizman kroiz at hyperroll.com
Wed Apr 4 07:20:57 EDT 2007


still, it is not impossible to write exception-safe. hard maybe but not
impossible.
after all I think most programers, will take ownership of the naked pointer.
so they will have to.
I also dont argue with this decision. I now understand it better. I just was
not familliar with auto_ptr.
thanks again.

On 4/4/07, Boris Kolpackov <boris at codesynthesis.com> wrote:
>
> Hi Guy,
>
> Guy Kroizman <kroiz at hyperroll.com> writes:
>
> > why is it impossible to write exception-safe code using naked pointers?
>
> This is a complicated issue. In a few words it has to do with the
> possibility of an exception being thrown while nobody "own" the
> naked pointer. The two situations in this case are:
>
> (1) One of the argument's destructor could throw. For example, assuming
>     foo() returns a dynamically allocated object as a naked pointer:
>
>     foo_t* f = foo ("foo.xml", 0 , xml_schema::properties ());
>
>     Here if the temporary for the third argument throws during destruction
>     the object will be leaked. This situation is somewhat far-fetched
>     because destructors normally do not throw.
>
> (2) The second situation is a lot more real. Suppose you have a
>     function that takes two arguments:
>
>     class bar_t {...};
>     void f (foo_t*, const bar_t&);
>
>     And you do something like this:
>
>     f (foo ("foo.xml"), bar ());
>
>     The C++ standard does not specify the order in which arguments
>     are evaluated. So it is possible that foo("foo.xml") will be
>     called first and bar() second. If bar's constructor throws,
>     then the object returned by foo() is leaked.
>
>
> > As you suggested before, I could keep the naked pointer as a member in a
> > class the will destroy it in the destructor.
> > that is exception-safe. right?
>
> It can be made exception-safe but it is a lot of work if you keep naked
> pointers around. It is easier to make each member clean-up after itself
> automatically by using smart pointers. For more on this see, for example,
> this C++ FAQ entry:
>
> http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.4
>
>
> hth,
> -boris
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.5 (GNU/Linux)
>
> iQGVAwUBRhN69MiAKQuuCE8dAQJGYQv+MsZNIq81igWWm6WA+yp/HJo1uTUnJ0F0
> qHXEjBzxd7f6JQ4kDtcKdHltdqQT28pWeXpqG1g3lxHNC16xVy/o3yWkbgcfYLbU
> FJunMWBEIz1/KAWICp15FC4nVuypMXy2eJg4btzdejFFDqV1sBdIjqcOQ4oMJfkt
> Ky9f7lFpWUmQOavHZGSVsAKdjTndLfekVeUytaOebireiVX7zYNVwYgi9iciS1+o
> SPoteSfVFEVMmX3jdgUmU+zqJreLWbbycy2surRSDz+8hAoAPA1e91H/xOE5Ibw9
> dpF5xkHLp9mWjAVze6NYRFuJBOLCOlGQAV6irDr6miwi86/fOfSKdtVZHpLbcERH
> cPejnlFmGht5dl2PH2idGYp6kSwC6XipnCLOvbroX9sHGbecvBBoKD1w4bDCjnhx
> 6v5F4hW+uwzmybOmLwm/4nMRZrnu2A9fbICNY1ZSj5WmUNso2JPTgRbmCRIVgt36
> KGc6i1KioZ+1fOr1gC8AC0mTfoJORwH+
> =WXWg
> -----END PGP SIGNATURE-----
>
>


-- 
Q: What do agnostic, insomniac dyslexics do at night?
A: Stay awake and wonder if there's a dog.



More information about the xsd-users mailing list