Delete Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Venn Syii

    Delete Problem

    I have the following line of code:

    if (MyData)
    {
    delete MyData;
    MyData= NULL;
    }

    Below is MyData:
    SomeDataStructu re *MyData;

    Every time this code segment is called I get a fatal error that causes my
    program to terminate.

    I use the debugger and upon reaching this line of code MyData is still valid
    and still has the correct information in it.
    I have searched the internet and pretty much figured that it was some sort
    of memory allocation problem... either I'm deleting what's not there, or
    even I've read deleting from different stacks? Any ideas as to why this
    delete cause and error?





  • Alf P. Steinbach

    #2
    Re: Delete Problem

    * Venn Syii:[color=blue]
    > I have the following line of code:
    >
    > if (MyData)
    > {
    > delete MyData;
    > MyData= NULL;
    > }[/color]

    It suffices to write


    delete MyData;

    [color=blue]
    >
    > Below is MyData:
    > SomeDataStructu re *MyData;
    >
    > Every time this code segment is called I get a fatal error that causes my
    > program to terminate.
    >
    > I use the debugger and upon reaching this line of code MyData is still valid[/color]

    *Seems* to be valid.
    [color=blue]
    > and still has the correct information in it.[/color]

    Any area of memory contains what it did earlier unless it has been
    changed...

    [color=blue]
    > Any ideas as to why this delete cause and error?[/color]

    Technically:

    * Your pointer MyData might be invalid, i.e. already deleted.

    * The object's destructor might reference invalid data or divide
    by zero or whatever.

    * The process might be in an invalid state (stack, memory manager,
    whatever) due to earlier errors.

    Design-wise:

    * You're using raw pointers instead of containers and smart-pointers.

    * You haven't ensured the class invariant through all operations.

    * You don't have a class invariant.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Victor Bazarov

      #3
      Re: Delete Problem

      Venn Syii wrote:[color=blue]
      > I have the following line of code:
      >
      > if (MyData)
      > {
      > delete MyData;
      > MyData= NULL;
      > }
      >
      > Below is MyData:
      > SomeDataStructu re *MyData;
      >
      > Every time this code segment is called I get a fatal error that causes my
      > program to terminate.
      >
      > I use the debugger and upon reaching this line of code MyData is still valid
      > and still has the correct information in it.[/color]

      How do you know it's "valid"? On what do you base your conclusion of
      its validity?
      [color=blue]
      > I have searched the internet and pretty much figured that it was some sort
      > of memory allocation problem... either I'm deleting what's not there, or
      > even I've read deleting from different stacks? Any ideas as to why this
      > delete cause and error?[/color]

      The most common mistake leading to such error is deleting the memory
      twice. After the first deletion the memory may still be OK (not reclaimed
      by the OS) which can be misinterpreted that the pointer is still valid.

      Another mistake, less common, is not allocating the memory in the first
      place but using an address of a static or automatic object. Since the
      memory wasn't allocated using 'new', it shouldn't be freed using 'delete'.

      Read FAQ 5.8.

      V

      Comment

      • Victor Bazarov

        #4
        Re: Delete Problem

        Alf P. Steinbach wrote:[color=blue]
        > * Venn Syii:
        >[color=green]
        >>I have the following line of code:
        >>
        >> if (MyData)
        >> {
        >> delete MyData;
        >> MyData= NULL;
        >> }[/color]
        >
        >
        > It suffices to write
        >
        >
        > delete MyData;[/color]

        No, it does not. Setting a pointer to NULL after deletion prevents any
        further double deletions of the same memory.
        [color=blue]
        > [...][/color]

        V

        Comment

        • Krishanu Debnath

          #5
          Re: Delete Problem

          Venn Syii wrote:
          [color=blue]
          >I have the following line of code:
          >
          > if (MyData)
          > {
          > delete MyData;
          > MyData= NULL;
          > }
          >
          >Below is MyData:
          >SomeDataStruct ure *MyData;
          >
          >Every time this code segment is called I get a fatal error that causes my
          >program to terminate.
          >
          >I use the debugger and upon reaching this line of code MyData is still valid
          >and still has the correct information in it.
          >I have searched the internet and pretty much figured that it was some sort
          >of memory allocation problem... either I'm deleting what's not there, or
          >even I've read deleting from different stacks? Any ideas as to why this
          >delete cause and error?
          >
          >
          >[/color]
          Looks like a memory access error. Why don't you run purify/valgrind ?

          Krishanu


          Comment

          • Venn Syii

            #6
            Re: Delete Problem

            Thanks for your fast answer. I've tracked it through the destructor and it
            get's through that fine. It's right after that it goes bad. I'm assuming
            more then likely it's option:
            1) Your pointer MyData might be invalid, i.e. already deleted.
            in conjunction with
            2)*Seems* to be valid.

            I'll dig through the code more to see.
            "Alf P. Steinbach" <alfps@start.no > wrote in message
            news:41af3c8f.2 650683125@news. individual.net. ..[color=blue]
            >* Venn Syii:[color=green]
            >> I have the following line of code:
            >>
            >> if (MyData)
            >> {
            >> delete MyData;
            >> MyData= NULL;
            >> }[/color]
            >
            > It suffices to write
            >
            >
            > delete MyData;
            >
            >[color=green]
            >>
            >> Below is MyData:
            >> SomeDataStructu re *MyData;
            >>
            >> Every time this code segment is called I get a fatal error that causes my
            >> program to terminate.
            >>
            >> I use the debugger and upon reaching this line of code MyData is still
            >> valid[/color]
            >
            > *Seems* to be valid.
            >[color=green]
            >> and still has the correct information in it.[/color]
            >
            > Any area of memory contains what it did earlier unless it has been
            > changed...
            >
            >[color=green]
            >> Any ideas as to why this delete cause and error?[/color]
            >
            > Technically:
            >
            > * Your pointer MyData might be invalid, i.e. already deleted.
            >
            > * The object's destructor might reference invalid data or divide
            > by zero or whatever.
            >
            > * The process might be in an invalid state (stack, memory manager,
            > whatever) due to earlier errors.
            >
            > Design-wise:
            >
            > * You're using raw pointers instead of containers and smart-pointers.
            >
            > * You haven't ensured the class invariant through all operations.
            >
            > * You don't have a class invariant.
            >
            > --
            > A: Because it messes up the order in which people normally read text.
            > Q: Why is it such a bad thing?
            > A: Top-posting.
            > Q: What is the most annoying thing on usenet and in e-mail?[/color]


            Comment

            • Alf P. Steinbach

              #7
              Re: Delete Problem

              * Victor Bazarov:[color=blue]
              > Alf P. Steinbach wrote:[color=green]
              > > * Venn Syii:
              > >[color=darkred]
              > >>I have the following line of code:
              > >>
              > >> if (MyData)
              > >> {
              > >> delete MyData;
              > >> MyData= NULL;
              > >> }[/color]
              > >
              > >
              > > It suffices to write
              > >
              > >
              > > delete MyData;[/color]
              >
              > No, it does not. Setting a pointer to NULL after deletion prevents any
              > further double deletions of the same memory.[/color]

              Well, we could argue about that.

              I've taken both sides (not simultanously, of course!) over the years.

              Currently, my thinking is that _if_ setting it to NULL prevents a double
              deletion, _then_ setting it to NULL most probably prevents detection of
              a serious bug.

              However, there can be other more valid reasons for setting a pointer to
              NULL after deletion, e.g. for optimization purposes. For example,
              deletions are rare, and this pointer is in the middle of an array of
              pointers; MyData above would then be a reference to that array element.
              Given the OP's definition of MyData I don't think that's the case here.

              In short, I disagree. ;-)

              --
              A: Because it messes up the order in which people normally read text.
              Q: Why is it such a bad thing?
              A: Top-posting.
              Q: What is the most annoying thing on usenet and in e-mail?

              Comment

              • Venn Syii

                #8
                Re: Delete Problem

                Heh... now I really feel newbie... what's purify/valgrind?


                ----- Original Message -----
                From: "Krishanu Debnath" <invalid@domian .com>
                Newsgroups: comp.lang.c++
                Sent: Thursday, December 02, 2004 10:07 AM
                Subject: Re: Delete Problem

                [color=blue]
                > Venn Syii wrote:
                >[color=green]
                >>I have the following line of code:
                >>
                >> if (MyData)
                >> {
                >> delete MyData;
                >> MyData= NULL;
                >> }
                >>
                >>Below is MyData:
                >>SomeDataStruc ture *MyData;
                >>
                >>Every time this code segment is called I get a fatal error that causes my
                >>program to terminate.
                >>
                >>I use the debugger and upon reaching this line of code MyData is still
                >>valid
                >>and still has the correct information in it.
                >>I have searched the internet and pretty much figured that it was some sort
                >>of memory allocation problem... either I'm deleting what's not there, or
                >>even I've read deleting from different stacks? Any ideas as to why this
                >>delete cause and error?
                >>
                >>
                >>[/color]
                > Looks like a memory access error. Why don't you run purify/valgrind ?
                >
                > Krishanu
                >
                >[/color]




                Comment

                • Victor Bazarov

                  #9
                  Re: Delete Problem

                  Alf P. Steinbach wrote:[color=blue]
                  > [...]
                  > In short, I disagree. ;-)[/color]

                  Why am I not surprised?...

                  Comment

                  • Mike Wahler

                    #10
                    Re: Delete Problem


                    "Venn Syii" <venn_syii@hotm ail.com> wrote in message
                    news:SNGrd.7861 0$ye4.101@twist er.rdc-kc.rr.com...[color=blue]
                    > I have the following line of code:
                    >
                    > if (MyData)
                    > {
                    > delete MyData;[/color]

                    What was used to allocate memory for 'MyData'? If it
                    was 'new[]' (not 'new'), then you need to write

                    delete[] MyData;


                    -Mike


                    Comment

                    Working...