Christopher Smith wrote:
using (Foo foo = new Foo(...))
{
  ...
}

What's not reliable there? Either foo gets initialized or it doesn't.

But Foo can get halfway through initialization, throw an error, and not be finalized by the time you leave the function.

using (Foo foo = new ComplexSqlSocket()) { .... }
CrashIfSqlSocketStillOpen();

If ComplexSqlSocket calles {new SqlSocket()} in its constructor, and then the constructor throws, neither ~SqlSocket() nor ~ComplexSqlSocket() gets invoked before you get to the CrashIfSqlSocketStillOpen.

In C++, it does, using the RIAA idiom. If a constructor throws after having constructed subcomponents, it deconstructs the subcomponents properly for you.

If it does, then finalize() is reliably called. If it doesn't, then
finalize() is reliably *not* called. Either way, entirely reliable.

Well, yes. It's stupidly defined to not do what you'd expect, which is that you always call the finalizer if the initializer completes.

--
  Darren New / San Diego, CA, USA (PST)
    His kernel fu is strong.
    He studied at the Shao Linux Temple.

--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to