when is object destroyed?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Herman

    when is object destroyed?

    I have the following loop:

    for (int i = 0; i < n; ++i)
    {
    CClass newObject( i );
    //
    // lines of code
    //
    }

    Since a new object is being created (is it?) for every iteration, when is
    the previous object being destroyed, or is it? If not, how do I destroy
    it? Do I need to, if I don't will this lead to a memory leak?

    -charles

  • keanu

    #2
    Re: when is object destroyed?

    Charles Herman wrote in <3faabb6f_2@127 .0.0.1>:
    [color=blue]
    > I have the following loop:
    >
    > for (int i = 0; i < n; ++i)
    > {
    > CClass newObject( i );
    > //
    > // lines of code
    > //
    > }
    >
    > Since a new object is being created (is it?) for every iteration, when is
    > the previous object being destroyed, or is it? If not, how do I destroy
    > it? Do I need to, if I don't will this lead to a memory leak?
    >
    > -charles[/color]

    it will be destroyed at the end of the scope
    at the }

    Comment

    • lilburne

      #3
      Re: when is object destroyed?

      Charles Herman wrote:
      [color=blue]
      > I have the following loop:
      >
      > for (int i = 0; i < n; ++i)
      > {
      > CClass newObject( i );
      > //
      > // lines of code
      > //
      > }
      >
      > Since a new object is being created (is it?) for every iteration, when is
      > the previous object being destroyed, or is it? If not, how do I destroy
      > it? Do I need to, if I don't will this lead to a memory leak?
      >[/color]

      Its created each time though the loop and each time through
      too (closing brace). Can be a potential bottleneck,
      particularly if it allocates any memory, but you won't know
      for sure unless you profile it.

      Comment

      Working...