Recent

Author Topic: Tidying up after constructor failure  (Read 3261 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Tidying up after constructor failure
« Reply #15 on: January 30, 2022, 08:32:25 pm »
Something like that ?
Code: Pascal  [Select][+][-]
  1. program Unit1;
  2. [/quote]
  3.  
  4. Something like this appears adequate:
  5.  
  6. [code=pascal]
  7. ...
  8.   finally
  9.     if not Assigned(self) then begin    (* Got here because of fail             *)
  10.  
  11. (* Fail deallocated the instance, so we have to rely on local variables for     *)
  12. (* handles etc.                                                                 *)
  13.  
  14.       if i2cHandle <> -1 then
  15.         fpClose(i2CHandle);
  16.       if powerDownLineHandle <> nil then
  17.         Gpiod.gpiod_line_release(powerDownLineHandle);
  18.       if powerDownChipHandle <> nil then
  19.         Gpiod.gpiod_chip_close(powerDownChipHandle)
  20.     end
  21.   end
  22. end { TMFRC522_I2C.Create } ;
  23.  

There's various things which tidy themselves up automatically, e.g. the state of the physical GPIO signal once it's released. There's also various "gotchas" in the library, e.g. an active_low routine that isn't implemented in early versions which is why I usually prefer to access this sort of thing using dynamic linkage (in the code above, Gpiod can either be a unit encapsulating load-time linkage or an object encapsulating explicit dynamic linkage).

So my question was fairly well focussed, and really boiled down to "can I trust this bit of documentation?" :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Tidying up after constructor failure
« Reply #16 on: January 30, 2022, 10:08:43 pm »
It explicitly asked about a finally block, and that handles etc. were involved. Hence my clarification above :-)
You did not specify where the finilization was (inside the constructor or outside).
My examples shows (implicitely) that it can be detected reliably in a finally outside the constructor.

Bart

 

TinyPortal © 2005-2018