Deleting a Polymorphic Class

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

    #1

    Deleting a Polymorphic Class

    Hi,[color=blue]
    >[/color]
    I'm having a debug assertion error within the file dbgdel.cpp with the
    expression:

    _BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)

    I traced the origin of the error and it happened as I tried to delete a
    polymorphic class as follows:

    class __declspec(dlle xport) A {
    public:
    char* name;
    A( ) { }
    virtual ~A( ) { delete name; }
    }

    class B {
    public:
    B( ) { }
    virtual ~B( ) { }
    }

    class C: public A, public B {
    public:
    C( ) { name = "I am C"; }
    ~C( ) { }
    }

    int main() {
    B* obj = new C;
    delete B;
    }


    This is done in VC 7.1. Can anyone help me solve my problem?

  • Jim Langston

    #2
    Re: Deleting a Polymorphic Class


    "FefeOxy" <jacksun007@gma il.com> wrote in message
    news:1138744233 .698897.91460@o 13g2000cwo.goog legroups.com...[color=blue]
    > Hi,[color=green]
    >>[/color]
    > I'm having a debug assertion error within the file dbgdel.cpp with the
    > expression:
    >
    > _BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)
    >
    > I traced the origin of the error and it happened as I tried to delete a
    > polymorphic class as follows:
    >
    > class __declspec(dlle xport) A {
    > public:
    > char* name;
    > A( ) { }
    > virtual ~A( ) { delete name; }
    > }
    >
    > class B {
    > public:
    > B( ) { }
    > virtual ~B( ) { }
    > }
    >
    > class C: public A, public B {
    > public:
    > C( ) { name = "I am C"; }
    > ~C( ) { }
    > }[/color]

    If this is the actual code, name = "I am C", then you are changing where the
    pointer is pointing to, and it's not being allocated with new. You only use
    delete if the memory was allocated with new, which wasn't in this case.
    Which would definately be the cause of your error.
    [color=blue]
    >
    > int main() {
    > B* obj = new C;
    > delete B;
    > }
    >
    >
    > This is done in VC 7.1. Can anyone help me solve my problem?
    >[/color]


    Comment

    • Alf P. Steinbach

      #3
      Re: Deleting a Polymorphic Class

      * FefeOxy:[color=blue]
      >
      > class __declspec(dlle xport) A {
      > public:
      > char* name;
      > A( ) { }
      > virtual ~A( ) { delete name; }
      > }
      >
      > class B {
      > public:
      > B( ) { }
      > virtual ~B( ) { }
      > }
      >
      > class C: public A, public B {
      > public:
      > C( ) { name = "I am C"; }
      > ~C( ) { }
      > }
      >
      > int main() {
      > B* obj = new C;
      > delete B;
      > }
      >
      >
      > This is done in VC 7.1. Can anyone help me solve my problem?[/color]

      First, "__declspec(dll export)" is not relevant and is not standard C++,
      and you're missing a bunch of semicolons: this code won't compile.

      At the very lowest technical level: you're trying to 'delete' something
      that wasn't allocated by 'new'.

      At a slightly higher level, you have a very confused or no intentional
      allocation of responsibilitie s. Be clear about responsibilitie s, choose
      what is responsible for that, and design that in. Also, make sure to
      initialize things, which you have forgotten above: there should never
      exist an uninitialized or partially uninitialized object anywhere.

      Hth.,

      - Alf

      --
      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

      • FefeOxy

        #4
        Re: Deleting a Polymorphic Class

        Thank you for your help. Is there a more relevent newsgroup that I
        should be going to?

        Comment

        • Alf P. Steinbach

          #5
          Re: Deleting a Polymorphic Class

          * FefeOxy:[color=blue]
          >
          > Thank you for your help. Is there a more relevent newsgroup that I
          > should be going to?[/color]

          No, this one's fine for C++ issues.

          I hope you got that about 'delete' requiring allocation by 'new' (or
          nullpointer).

          Cheers,

          - Alf

          --
          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

          • Peter_Julian

            #6
            Re: Deleting a Polymorphic Class


            "FefeOxy" <jacksun007@gma il.com> wrote in message
            news:1138744233 .698897.91460@o 13g2000cwo.goog legroups.com...
            | Hi,
            | >
            | I'm having a debug assertion error within the file dbgdel.cpp with the
            | expression:
            |
            | _BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)
            |
            | I traced the origin of the error and it happened as I tried to delete
            a
            | polymorphic class as follows:
            |
            | class __declspec(dlle xport) A {
            | public:
            | char* name;
            | A( ) { }
            | virtual ~A( ) { delete name; }
            | }

            Deleting a stack allocation is undefined behaviour. That should have
            been expected.

            class A
            {
            std::string m_s;
            public:
            A(std::string s) : m_s(s) { }
            virtual ~A() { }
            std::string getS() const { return m_s; }
            };

            |
            | class B {
            | public:
            | B( ) { }
            | virtual ~B( ) { }
            | }

            class B
            {
            public:
            B() { }
            virtual ~B() { }
            };

            |
            | class C: public A, public B {
            | public:
            | C( ) { name = "I am C"; }
            | ~C( ) { }
            | }

            class C : public A, public B
            {
            public:
            C(std::string s) : A(s) { }
            ~C() { } // virtual
            };

            |
            | int main() {
            | B* obj = new C;
            | delete B;
            | }

            the instance created above is not B, it's obj. One does not
            allocate/deallocate a type. Types don't exist.

            int main()
            {
            B* obj = new C("a string");
            std::string s(obj->getS());
            delete obj; // deletes the object at obj, not the pointer
            }

            Where is all this confusion about types, instances and pointers coming
            from? The distinction between a user-type and a variable of this
            user-type is *critical* (i can't find a better word).

            A drafstman or architect can draw a house on paper as many times as he
            likes, he still doesn't have a house. A constructor needs to build the
            house using a specific blueprint (a class or struct) before there is a
            house to speak of. The house is a variable of that blueprint.

            The whole idea about polymorphism is that the instance *at* obj knows
            whether its an A, a B or a C. In other words, if you create a container
            of pointers and initialize the pointers to valid objects, its the object
            pointed_to, not the container, that knows whether its of type B or C.
            The appropriate virtual destructor(s) is/are therefore invoked upon
            destruction.

            int main()
            {
            std::vector< B* > vpb; // a container of pointers
            vpb.push_back( new B );
            vpb.push_back( new C("string_2") );
            vpb.push_back( new C("string_3") );

            for (int i = 0; i < vpb.size(); ++i)
            {
            delete vpb[i];
            }
            // first pass: ~B()
            // second pass: ~B() and ~C() are invoked
            // third pass: ~B() and ~C() are invoked

            // note: at this point you still have a vector of 3 elements.
            // The vector doesn't know, nor care, that the 3 pointers
            // are now undefined.

            } // the vector and its pointers are destroyed here automatically.



            Comment

            Working...