Re: [patch] Fix crash in read_pe_exported_syms

Sergio Durigan Junior
2013-03-03T23:06:30Z

Hi Pierre,

On Saturday, March 02 2013, Pierre Muller wrote:

> I still didn't really get a fully correct picture of the correct way to handle
> the cleanups, but I think that your analysis is correct and that this patch should
> be approved by a global maintainer.

Not sure if you meant that you want to understand how cleanups work, but
anyway...

Think of cleanups as a stack. When you call `make_cleanup', you are
asking for a new operation to be put in the stack, and you receive the
pointer to the last operation in that stack (before yours, which becomes
the new head). When you call `do_cleanups', you are telling GDB to
iterate over the stack in reverse and execute each operation until the
one you provide as the argument of the function. `discard_cleanups'
does a similar job, but instead of executing each operation, it discards
them.

When you know that you will have many cleanups and don't want to keep
track of each one individually, you can use the same trick as
`read_pe_exported_syms': you register a dummy cleanup (null_cleanup,
which does nothing) and then start calling `make_cleanup' without caring
about its return value. Then, when the time comes (e.g., you are about
to return from the function), you can either call `do_cleanups' or
`discard_cleanups' using as the argument the one you received in the
first call to `make_cleanup' with a dummy cleanup, and you'll be sure
that all the cleanup chain will be executed or discarded up to the point
where you started your function.

Sorry if you already knew all that and I misunderstood your question
:-).

> PS: It would also be better that this goes in before branching 7.6!

I agree.

--
Sergio