This is the mail archive of the gdb@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Will therefore GDB utilize C++ or not?


On 04/18/2012 09:24 PM, Tom Tromey wrote:

> Pedro> Even RAII is not _that_ different from cleanups.
> 
> No, it really is.  It is not dynamic, and you don't have to remember to
> call do_cleanups on all exit paths.


Cleanups are dynamic because they're been crafted that way.
We could tweak cleanups to allow creating them on the stack.

What I meant by not that different, is, that with RAII you get
to write something like (the dumbest version of a RAII object
that supports canceling possible):

class my_raii_thing
{
private:
   whatever_arg *arg; << moral equivalent of the cleanup args.
   bool discarded;

public:
   my_raii_thing (whatever_arg *p) : arg (p), discarded(false)
   {}

   ~my_raii_thing () << moral equivalent of a cleanup function.
   {
      if (!discarded)
        {
          // whatever to release this->arg or something like that.
        }
   }

   void discard ()
   {
      discarded = true;
   }
};

and then do:

  my_raii_thing foo (&whatever_arg);

  if (whatnot)
    {
       whatever_arg.discard();
       return SUCESS;
    }
  }

whereas we now write:

struct cleanup_args
{
  ...
};

void foo_cleanup (void *arg)
{
   // whatever to release this->arg or something like that.
}

and then:

  old_chain = make_cleanup (foo_cleanup, &whatever_arg);

  if (whatnot)
    {
       discard_cleanups (old_chain);
       return SUCESS;
    }
  do_cleanup (old_chain);

But yes, as I've said before elsewhere, give me destructors,
everything else I can live without.  :-)

Basically, whoever understands RAII should understand cleanups.
Not counting the auto-destruction issue, I'm saying that writing
raii classes vs writing cleanup function is mostly syntax sugar.

> 
> I've fixed any number of memory (and, IIRC, file descriptor) leaks
> caused precisely because cleanups are less good than RAII.
> 
> Basically, with cleanups the default is full generality, but this is
> wrong.  With RAII the default is block scoping, and you have to expend
> effort to do weird stuff.


We could make make_cleanup return a reference to this instead of to the
previous (as Doug suggested not long ago), and tweak cleanups to make
it possible to have them on the stack, making their use follow scope
closer too.

-- 
Pedro Alves


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]