Sorry, I'm obviously not able to handle my mail-program correctly, I wanted to
forward my answer that I accidently send Beth directly and not the mailing-list.

Anyway, now my answer:

"Savoy, Elizabeth S" wrote:

> In an SDF star, on Linux...
>
> If I create an object in a star's go method using
>
>         MyClass *O = new MyClass();
>
> but I never put this object into an envelope or send it to output (it is not
> a Ptolemy "message"), do I still need to delete it within that star?  If so,
> how and where?  I would assume I would use
>
>         delete O;
>
> in the go method, but this causes a core dump.
>
> Any help is appreciated.
> Thanks
>
> Beth Savoy
> Corning, Inc
> (607) 974-8813
>
> ----------------------------------------------------------------------------
> Posted to the ptolemy-hackers mailing list.  Please send administrative
> mail for this list to: [EMAIL PROTECTED]

Hi Beth,

The methods in Ptolemy stars have are executed as follows:

        constructor:    executed once to create a star instance

        setup:          executed at least once, possibly more than
                        once, during universe startup

        begin:          executed exactly once during startup
                        (after setup calls)

        go:             executed zero or more times during a run

        wrapup:         executed once during successful termination
                        of a run; *not* executed if run fails

        destructor:     executed once when star instance is destroyed

This is a fragment of one of my stars which shows how to handle dynamic memory,
maybe it helps you. It is important to notice that deleting multiple times a
pointer which is pointing to "0" produces no failure, whereas deleting a
pointer more than once which points to a real memory address causes a crash at
the second time. This is the reason for setting the pointers to zero in the
constructor (because we need to delete the memory in setup) and in wrapup.

---------------------------------------------------------
 defstate {
  name {Size}
  type {int}
  desc { Size of Array. }
 }
  defstate {
    name { fileName }
    type { string }
    default { "<stdout>" }
  }
  protected {
    int * t;
    pt_ofstream * p_out;    // output stream to 'cout' or file
  }
  hinclude { "pt_fstream.h" }
  constructor {
        p_out = 0;
        t = 0;
  }
  destructor {
        LOG_DEL; delete p_out;
        LOG_DEL; delete [] t;
  }
  setup {
    // in case file was open from previous run w/o wrapup call
    LOG_DEL; delete p_out;
    LOG_NEW; p_out = new pt_ofstream(fileName);

    LOG_DEL; delete [] t;
    LOG_NEW; t = new int[Size];
  }
  wrapup {
    // give dynamically allocated memory back to system:
    LOG_DEL; delete p_out; p_out = 0;
    LOG_DEL; delete [] t; t = 0;
  }
--------------------------------------------------------------------------

______________________________________________________________________________
Kai Below

TU Hamburg-Harburg, Digital Communication Systems
Denickestr. 17, R. 3009, D-21071 Hamburg, G E R M A N Y

Tel: +4940 428 78 - 3351, Fax: +4940 428 78 - 2941
http://www.tu-harburg.de/~et6kb/ (PGP-key available)
______________________________________________________________________________




----------------------------------------------------------------------------
Posted to the ptolemy-hackers mailing list.  Please send administrative
mail for this list to: [EMAIL PROTECTED]

Reply via email to