[fpc-devel] finalization

J. Gareth Moreton gareth at moreton-family.com
Tue Jan 22 10:08:09 CET 2019


 Hi Alexander,

 Pressing Ctrl+C or Ctrl+Break is an interrupt that breaks normal program
flow - if unhandled, it causes an abnormal program termination and things
don't get cleaned up properly, which includes calling finalization blocks.

 Try doing these steps to get around it:

 - Include the "SysUtils" unit.  This causes the Ctrl+C signal to raise an
exception which can then be trapped.
 - Instead of using 'finalization', wrap your main procedure with a
'try...finally' block, calling "spkoff" in the finally section.

 As an extra note... if you specify the "assembler" directive after your
procedure header, you don't need to use "begin...end" and can just use
"asm...end" directly.
 Let me know how it goes.

 Gareth aka. Kit

 On Tue 22/01/19 09:11 , "Alexander via fpc-devel"
fpc-devel at lists.freepascal.org sent:
 Hi, 

 I try use finalization in unit, but it not work. 

 When press Ctrl+C and exit program uses this unit 
 PC-Speaker stay infinity play on interrupted tone. 
 When Ctrl+C use spkoff; in finalization section not called. 

 Unit: 

 unit spkunit; 

 {$MODE OBJFPC} 
 {$ASMMODE INTEL} 
 {$CODEPAGE UTF8} 

 { 
 Unit for playing melodys on PC-Speaker. 
 For GNU/Linux 64 bit version. Root priveleges needed. 
 Written on FreePascal. 
 Copyright (C) 2019 Artyomov Alexander 

 This program is free software: you can redistribute it and/or modify 
 it under the terms of the GNU Affero General Public License as 
 published by the Free Software Foundation, either version 3 of the 
 License, or (at your option) any later version. 

 This program is distributed in the hope that it will be useful, 
 but WITHOUT ANY WARRANTY; without even the implied warranty of 
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 GNU Affero General Public License for more details. 

 You should have received a copy of the GNU Affero General Public License 
 along with this program. If not, see  [1];. 
 } 

 interface 

 uses X86; 

 procedure spk(b : word ); procedure spkon; procedure spkoff; 

 implementation 

 procedure spkon; 
 begin 
 asm 
 push rax 
 in al, 61h 
 or al, 03h 
 out 61h, al 
 pop rax 
 end; 
 end; 
 procedure spkoff; 
 begin 
 asm 
 push rax 
 in al, 61h 
 or al, 03h 
 xor al, 03h 
 out 61h, al 
 pop rax 
 end; 
 end; 
 procedure spk(b : word ); 
 var hb, lb : byte; 
 begin; hb := hi (b); lb := lo (b); 
 asm 
 push rax 
 mov al, 0B6h 
 out 43h, al 
 mov al, lb 
 out 42h, al 
 mov al, hb 
 out 42h, al 
 pop rax 
 end; 
 end; 

 initialization 

 fpioperm($42, 2, 1); // fpioperm($42, 1, 1); fpioperm($43, 1, 1); 
 fpioperm($61, 1, 1); 

 finalization 

 spkoff; 

 end. 

 _______________________________________________ 
 fpc-devel maillist - fpc-devel at lists.freepascal.org 
 http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
[2]">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel 

 

Links:
------
[1]
http://secureweb.fast.net.uk/parse.php?redirect=https://www.gnu.org/licenses/%26gt
[2] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190122/3e79dd94/attachment.html>


More information about the fpc-devel mailing list