Thread: delete isn't working for me!

  1. #1
    Unregistered
    Guest

    Question delete isn't working for me!

    OK, So there I was writing my interesting program that uses lib's, if you haven't heard about it, read my other thread...
    ok, anyway, here's the story:

    I'm doing a class, which uses a constructor and a destructor as follows:

    class cMyWindow
    {
    private:
    .
    HWND hWnd;
    char* title;
    .
    blah blah
    .
    .
    .

    public:
    cMyWindow(HWND hwnd, const char Title);
    ~cMyWindow()
    .
    .
    blah blah
    .
    .
    };


    cMyWindow::cMyWindow(HWND hwnd, const char Title)
    {
    this->hWnd=hwnd;
    this->title=new(char[strlen(Title)+1]);
    strcpy(this->title,Title);
    }

    ~cMyWindow()
    {
    this->hWnd=0;
    delete this->hWnd; <------THE PROBLEM GOES HERE
    }

    the program compiles perfectly, but when It runs over the "delete", it exits with a horrible assertion error
    I don't know how to fix this!

    By the way, I'm using Visual C++ 6.0 Enterprise Edition(Don't tell me not to use it because it costed me a fortune)

    If anybody knows how to fix this problem, or at least knows why this is happening, tell me!

    Oskilian

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    It's because a window handle is NOT a C++ object -- you can't use new or delete with it.

  3. #3
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    Nonononono
    my mistake

    it was a typing mistake, the correct one must be

    delete this->title; <------THE PROBLEM GOES HERE

    sorry

    (the first message is mine, but I forgot to log in before writing it)

    Oskilian

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Hmm... did you try using delete[] in place of delete?

    I have used code much like this before, without having problems. I never use parentheses with new, but it should work the same either way.

    Also, you don't need to do things like "this->title" -- just saying "title" is the same.

  5. #5
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    I'll try it
    i knew that the this->title isn't necessary, butt it makes my code easier to understand (anyway, when it compiles, it removes the this.

    But thank you, I'll try it

    Oskilian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  2. delete by copy only for non-children nodes?
    By patricio2626 in forum C++ Programming
    Replies: 17
    Last Post: 11-18-2006, 08:37 AM
  3. Replies: 17
    Last Post: 11-16-2006, 09:06 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. how to delete objects
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2001, 02:06 AM