DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: Delete

  1. #1
    Join Date
    May 2004
    Posts
    242

    Delete

    class Shape
    {
    public:
    Shape() { }
    virtual ~Shape() { }
    virtual void draw() = 0;
    };

    class Circle : public Shape
    {
    public:
    Circle() { }
    virtual ~Circle() { delete this; }
    void draw() { cout << "Drawing Circle\n"; }
    };

    main()
    {
    Circle x;
    Circle *ptr= &x;
    ptr->draw();
    delete ptr;
    }

    Can someone please tell me whether the delete in the destructor is correct ? The code works, but when I'm debugging I'm getting Unhandled Exception messages.

  2. #2
    Join Date
    Sep 2006
    Posts
    14
    You are trying to delete the object twice(in fact thrice), so you got the exception, objects destroyed automatically in the destructor, so no need to call "delete this" , "delete ptr" .

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Never call delete this in a destructor! The destructor doesn't know whether *this is an automatic object, a static object or one that was allocated using new.
    Secondly, you should never call delete with a pointer to a local automatic object, as is ptr. The object to which ptr is pointing, x, is automatically destroyed.
    Danny Kalev

Similar Threads

  1. delete or delete []??
    By funwithdolphin in forum C++
    Replies: 2
    Last Post: 11-15-2005, 11:24 AM
  2. help reg creating ms access file
    By vbcoder in forum VB Classic
    Replies: 9
    Last Post: 04-28-2005, 12:36 PM
  3. DELETE from Multiple Tables with same WHERE
    By Arthur Wood in forum VB Classic
    Replies: 3
    Last Post: 04-01-2002, 03:14 PM
  4. Cascade delete vs delete trigger
    By Michael Culley in forum VB Classic
    Replies: 6
    Last Post: 02-12-2002, 03:30 AM
  5. trigger and cascade delete
    By Ted McNeal in forum Database
    Replies: 5
    Last Post: 12-07-2000, 01:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links