Re: Good style for memory management?





On 3/18/07, Paul Davis <pjdavis engineering uiowa edu> wrote:

idea is make construction a two step process.  This way the destructor
will always be called and always free resouces. I believe this is the
wrong answer.

Well, what you said, and additionally if intialization and resource allocation in the ctor isn't complete, but the
dtor gets called anyway, you'll sort of end up with comparable problems like checking what actually *has* been
allocated and what has to be released again; obviously this isn't standard procedure for writing a dtor (i for one
always am writing dtors assuming that the object has been fully constructed), so basically in all my ctors i do the
same what i think we both suggested to Paul, namely making sure that something doesn't throw and if it does, catch
it in the ctor, make sure we free all resources, and then there's always just throw; to let the caller know about the exception
if it's neccessary for it to know.

Well long speech point being, it does not even matter whether it is *inside* the ctor or outside of it, but Paul's original example
with creating a new dynamically instance, initializing it with whatever in between, and then only adding it to a parent container *is really bad*.

I wouldn't do that with gtkmm, nor gtk+ for that matter to the extent it's possible there (no exceptions but you can still always forget to add
the widget to a container). Gtk+ basically imposes on you to add it to a container to manage the widget itself so this should be done ASAP
after construction (e.g., now to the "original" Paul: Design your widget so it can remain hidden until it has been setup trough the ::init() method, but add it to the container already before calling your ::init() )

Milosz


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]