ats

How are strings freed in ATS2?


http://www.ats-lang.org/Documents.html includes "Introduction to Programming in ATS", which includes the assertion that fileref_get_line_string returns a Strptr1 (a look in filebas.dats shows that it returns a String via strptr2string), and it includes this code:

#include "share/atspre_staload.hats"
#include "share/atspre_staload_libats_ML.hats"

implement main0() = loop() where
  fun loop(): void = let
      val isnot = fileref_isnot_eof(stdin_ref)
    in
    if isnot then let
      val line = fileref_get_line_string(stdin_ref)
      val () = print_string(line)
      val () = strptr_free(line)
    in
      loop()
    end else ()
  end
end

Which throws a type error if the strptr_free line is included. If that line isn't included, the program blatantly leaks memory. Is there current documentation or are there ATS2 examples that show how the fileref_* words are supposed to be used? What is the ATS2 version of the code above?


Solution

  • There are two versions of fileref_get_line_string: one in prelude/filebas and the other in libats/ML/filebas. For getting linear strings, you need the former:

    #include
    "share/atspre_staload.hats"
    
    implement
    main0() = loop() where
      fun
      loop(): void = let
        val
        isnot =
        fileref_isnot_eof(stdin_ref)
      in
        if isnot then let
          val line =
          fileref_get_line_string(stdin_ref)
          val () =
          print_strptr(line)
          val () = free(line)
        in
          loop()
        end else ()
      end
    end