Search | Synergex.com | Contact Us | Resource Center Login | Home|
News

What is the output of the following program?

namespace mortality
  public class mortal
    method ~mortal
    record
        name    ,a30
        line    ,i4
    proc
      xcall modname(1, name, line)
      writes(1, %atrim(name) + ":" + %string(line))
    end
  endclass

endnamespace

main
proc
    open(1,o,"TT:")
    xcall life
    stop
end

subroutine life
record
    human       ,@mortal
proc
    human = new mortal()
    xreturn
end

a. MORTALITY.MORTAL.~MORTAL():8
b. LIFE:27
c. :0
d. You can’t rely on this code’s output.

Explanation
The class mortal has a destructor that uses the system-supplied routine MODNAME to report the context in which it is called. If the first argument to MODNAME were a 0, then it would report the location at which MODNAME itself was called (a). But because the first argument is 1, this routine reports the location at which the destructor is invoked. Destructors are called whenever the object system releases an object. In the current version of Synergy/DE, that happens at line 27 in routine LIFE (b), because the variable human goes out of scope at that point. But you cannot rely on that behavior.  Optimization of garbage collection may defer invoking the destructor until a later time – even as late as after a STOP statement. In fact, with the beta version of Synergy .NET (Synergy/DE for the .NET Framework) you get result (c), because the finalizer is invoked outside of the context of any routine. Thus, (d) is the correct answer.

Because you cannot be certain about when a destructor will be called, you must be very careful about what code a destructor contains.  For instance, if you use a destructor to delete a window, that code could be executed after the window system has been shut down. You must either guard against these conditions, or provide a separate method for releasing resources so you can invoke it explicitly within the required context.

More information about News and Events

Contact Synergex to learn more about  News and Events