Eiffel "Gotcha" #10 - cleaning up 
Author Message
 Eiffel "Gotcha" #10 - cleaning up

Suppose you want to make sure that a routine 'clean_up' is executed
immediately before your application exits. Is it sufficient to
make sure that a call to 'clean_up' is the last instruction of
the creation feature of your root class?

  class MAIN
  creation make
  feature
    make is
      do
        ...
        clean_up
      end
  end

No, because the application may exit due to an unhandled exception.
So maybe this is enough:

  class MAIN
  creation make
  feature
    make is
      do
        ...
        clean_up
      rescue
        ...
        clean_up
      end
  end

In this case, even an un-retried exception will result in a call to
'clean_up' before execution leaves the rescue clause and the exception
propagates to the run-time default exception handler.

But it turns out this is not enough - what's the problem?

Answer tomorrow.

Regards,
Roger
--
--
-- Roger Browne, 6 Bambers Walk, Wesham, PR4 3DG, UK | Ph 01772-687525
-- Everything Eiffel: http://www.*-*-*.com/ | +44-1772-687525



Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - cleaning up



Quote:
> But it turns out this is not enough - what's the problem?

Exception in the rescue clause (prior to clean_up)?

--

Aldur Systems Ltd, UK



Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - cleaning up


Quote:

>Suppose you want to make sure that a routine 'clean_up' is executed
>immediately before your application exits. Is it sufficient to
>make sure that a call to 'clean_up' is the last instruction of
>the creation feature of your root class?

[...]

Quote:
>No, because the application may exit due to an unhandled exception.
>So maybe this is enough:

[...]

Quote:
>But it turns out this is not enough - what's the problem?

If any of your classes inherit from MEMORY and redefine "destroy" (I think
its called) then that routine can be called after everything else terminates.

Indeed, a strict reading of the spec would *require* it to be called after
the root object dies (assuming that an instance was alive up until then)
because it has to be called before the object is collected, and final
collection will occur as the application terminates.

Paul.

--
Paul Johnson            | GEC-Marconi Ltd is not responsible for my opinions. |
+44 1245 242244         +-----------+-----------------------------------------+




Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - cleaning up

Quote:
> Suppose you want to make sure that a routine 'clean_up' is executed
> immediately before your application exits. Is it sufficient to
> make sure that a call to 'clean_up' is the last instruction of
> the creation feature of your root class?

>   class MAIN
>   creation make
>   feature
>     make is
>       do
>         ...
>         clean_up
>       end
>   end

> No, because the application may exit due to an unhandled exception.
> So maybe this is enough:

>   class MAIN
>   creation make
>   feature
>     make is
>       do
>         ...
>         clean_up
>       rescue
>         ...
>         clean_up
>       end
>   end

> In this case, even an un-retried exception will result in a call to
> 'clean_up' before execution leaves the rescue clause and the exception
> propagates to the run-time default exception handler.

> But it turns out this is not enough - what's the problem?

The problem is this feature from class EXCEPTIONS:

  die(code: INTEGER)
    -- Terminate execution with exit status 'code',
    -- without triggering an exception

If a call is made to 'die', execution will never reach the end of the
creation feature, nor of any rescue clause associated with it. Therefore,
'clean_up' will not be executed.

Paul Johnson suggested:

Quote:
> If any of your classes inherit from MEMORY and redefine "destroy" ...
> then that routine can be called after everything else terminates.

> Indeed, a strict reading of the spec would *require* it to be called after
> the root object dies (assuming that an instance was alive up until then)
> because it has to be called before the object is collected, and final
> collection will occur as the application terminates.

The description of 'die' in the Eiffel Library Standard, Vintage
95 (ELS95) says only what I quoted above, but the common English meaning of
'die' suggests that a reasonable behaviour is to "Terminate execution
immediately...", in which case 'destroy' will not be called for the
root class. I tried Visual Eiffel 2.0 gamma and that's how it works (I've
not tried it on any others). Yet, for 'destroy' to be really useful the
user needs to be confident that it will be called no matter what happens.

I think the only solution is a methodological one - "Never Say Die".
Or, if you really feel you must call 'die', do so only from the end of
your 'clean_up' routine. Routine 'die' is after all a 'goto' by another name.

Regards,
Roger
--
--
-- Roger Browne, 6 Bambers Walk, Wesham, PR4 3DG, UK | Ph 01772-687525
-- Everything Eiffel: http://www.eiffel.demon.co.uk/ | +44-1772-687525



Fri, 12 Nov 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Eiffel "Gotcha" #10 - discounts

2. Eiffel "Gotcha" #12 - onceness and invariants

3. Eiffel "Gotcha" #9 - postconditions

4. Eiffel "Gotcha" #7 - redundant parentheses

5. RH6.1 coredump: float("10.01")

6. A question about "cryptoaritmetical puzzle "(in swi prolog 5.0.10)

7. functional language "clean"

8. "exec" not memory clean

9. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

10. BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}

11. Building a "record" in Eiffel

12. Obvious Fake (was "Record in Eiffel")

 

 
Powered by phpBB® Forum Software