[fpc-pascal] Freeing memory with exceptions
Hairy Pixels
genericptr at gmail.com
Sun May 21 03:09:43 CEST 2023
I've never really used exceptions myself in Pascal (with the exception of breaking out of deeply recursive function calls) so I don't know all the rules.
In this example lets say you call Test() which allocates some memory and then calls out to another function in another unit which raises (the programmer doesn't know this because FPC doesn't mark the function as throwing exceptions). Now the Test() function will exit early before freeing the memory.
What are you supposed to do here? The only thing I can think of is to wrap every function in try..finally which COULD raise an exception but that's a huge mess because literally any function could raise.
====================================
procedure DoThis;
begin
raise Exception.Create('dead');
end;
procedure Test;
begin
TObject.Create;
// call some code in other unit which raise an exception
DoThis;
end;
begin
try
Test;
except
writeln('failed'); // TObject now leaks
end;
end.
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list