Oberon Community Platform Forum
November 22, 2019, 06:05:46 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Object finalization in A2  (Read 4673 times)
Alexey
Newbie
*
Posts: 27


« on: January 21, 2009, 12:02:52 PM »

Hello A2 developers!

I have the following questions about object finalization:

1. What is the correct way of setting a finalizer for an object in A2?

fn: Heaps.Finalizer;

NEW(fn); fn.finalizer := FinalizerProc;
Heaps.AddFinalizer(obj,fn);

Is this correct?

2. When the user frees a module, does it mean that the system finalizes all object instances created by this module and calls user defined object finalizer procedures?

Thank you,

Alexey
Logged
negelef
Administrator
Jr. Member
*****
Posts: 55


« Reply #1 on: January 21, 2009, 01:21:12 PM »

1. Yes, as long as fn is of type Heaps.FinalizerNode

2. SystemTools.Free invalidates references to a module. Objects and therefore also modules are freed by the garbage collector when they are not referenced anymore. Only then their finalizers are called.

« Last Edit: January 21, 2009, 01:43:09 PM by negelef » Logged
Alexey
Newbie
*
Posts: 27


« Reply #2 on: February 02, 2009, 10:48:04 PM »

Hi,

Yet another question concerning the same thing. I have realized that the system during the freeing of a module discards all object finalizers registered in the module. This I found by running Aos.Exe with debugger. When I free a module with registered object finalizers I see in the trace:

"Module name" N discarded finalizers, M pending finalizers

Is this final decision? What is the reason for this decision?

For me, in particular, it makes a problem. When a module is going to be freed, I have to release all resources allocated by objects created by the module. Since I can not use finalizers in this case, I have to implement a bookkeeping, which assumes object referencing and does not allow automatic garbaging of the objects by the system.

Thanks,
Alexey

 
Logged
Alexey
Newbie
*
Posts: 27


« Reply #3 on: February 04, 2009, 09:20:45 PM »

Nobody can give an answer?  Undecided
Logged
felix
Administrator
Newbie
*****
Posts: 17



WWW
« Reply #4 on: February 10, 2009, 04:55:32 PM »

Dear Alexey,

There is basically the following problem: assume the finalizer F of object A points to a procedure P in module M. Assume that the procedure P is getting invalid when M is unloaded - then the finalizer F (pointing to P) cannot be called any more. But P can also not be simply and preventively called for all objects with finalizer pointing to P since some of them might stay alive after M is closed - unless the user / programmer really would really want this behaviour explicitely.
And - volia - the solution is already built in Aos/A2: use your own Kernel.FinalizedCollection to enter finalizers and you can clean up (alive) objects when the finalizer gets invalid without having to do much of a bookkeeping by yourself. Actually, a finalized collection is invented exactly for this purpose. (Example below).
By the way: Using a finalized collection is cleaner anyway. Heaps.Mod should not be used in application programs.

MODULE TestFinalizers;

IMPORT KernelLog, Kernel,Modules;

VAR f: Kernel.FinalizedCollection;

VAR a,b,c: OBJECT END;

PROCEDURE Finalizer*(obj: ANY);
BEGIN
   KernelLog.String("finalized"); KernelLog.Ln;
END Finalizer;

PROCEDURE Test*;
BEGIN
   NEW(a); NEW(b); NEW(c);
   f.Add(a,Finalizer);
   f.Add(b,Finalizer);
   f.Add(c,Finalizer);
END Test;

PROCEDURE CleanupNode(obj: ANY; VAR cont: BOOLEAN);
BEGIN
   Finalizer(obj);
   cont := TRUE;
END CleanupNode;

PROCEDURE Cleanup;
BEGIN
   f.Enumerate(CleanupNode);
END Cleanup;

BEGIN
   NEW(f);
   Modules.InstallTermHandler(Cleanup)
END TestFinalizers.

TestFinalizers.Test ~
SystemTools.Free TestFinalizers ~


Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!