Exception when calling delete
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: Exception when calling delete

Hybrid View

  1. #1
    Join Date
    Dec 2003
    Location
    India
    Posts
    6

    Question Exception when calling delete

    Hi,
    When i am trying to run the code below,it gives runtime<br>
    exception. Although i am ctching the exception.<br>
    Plz suggest something. I cann't check the pointer against<br>
    NULL.


    Code:
    #include<iostream>
    #include<stdexcept>
    
    using namespace std;
    
    void main()
    {
    	char* p;
    	try
    	{
    		delete p;
    	}
    	catch(exception ex)
    	{
    		cout<<"hello"<<endl;
    	}
    }
    Regards<br>
    Ram

  2. #2
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515

    Re: Exception when calling delete

    Originally posted by Ram_Gupta
    Hi,
    When i am trying to run the code below,it gives runtime<br>
    exception. Although i am ctching the exception.<br>
    Plz suggest something. I cann't check the pointer against<br>
    NULL.


    Code:
    #include<iostream>
    #include<stdexcept>
    
    using namespace std;
    
    void main()
    {
    	char* p;
    	try
    	{
    		delete p;
    	}
    	catch(exception ex)
    	{
    		cout<<"hello"<<endl;
    	}
    }
    Regards<br>
    Ram
    You've not allocated anything with new.
    try:
    Code:
    char* p = new char[100];
    delete[] p;
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  3. #3
    Join Date
    Dec 2003
    Location
    India
    Posts
    6
    Hi,

    I agree with u. Actually it was a test code. In original one
    class is having some pointer data member. In the destructor
    he is freeing the memory but doesn't assign the pointer to
    NULL. So somehow he has assigned the object to someone
    else, so that pointer has become a dangling one. I don't
    want to change his overloaded = oparetor nor do i wanna
    provide a copy ctor. So i wanna call delete, catch the exception
    and cooly come out without any runtime error.

    Thanx and Regards
    Ram

  4. #4
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Originally posted by Ram_Gupta
    Hi,

    I agree with u. Actually it was a test code. In original one
    class is having some pointer data member. In the destructor
    he is freeing the memory but doesn't assign the pointer to
    NULL. So somehow he has assigned the object to someone
    else, so that pointer has become a dangling one. I don't
    want to change his overloaded = oparetor nor do i wanna
    provide a copy ctor. So i wanna call delete, catch the exception
    and cooly come out without any runtime error.

    Thanx and Regards
    Ram
    Why do you want to call delete on something that you know is a "dangling pointer"?
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471
    Just to recap: the example code given by the OP invokes undefined behaviour, since the delete is called on a pointer that has not been allocated by new and it is not NULL.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Dec 2003
    Location
    India
    Posts
    6
    Hi,

    I just want to know whether i can catch an exception
    if call delete on dangling pointer. B'coz in that case my
    job would be less tiresome.

    Thakx and Regards
    Ram.

  7. #7
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Originally posted by Ram_Gupta
    Hi,

    I just want to know whether i can catch an exception
    if call delete on dangling pointer. B'coz in that case my
    job would be less tiresome.

    Thakx and Regards
    Ram.
    Yes.
    You can catch the exception.
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471
    To reiterate:

    It is undefined behaviour

    This means that, just because this particular compiler (or version of the compiler) throws an exception, you can't rely on that. The next upgrade to your compiler may do something different. For example, it may just call exit() immediately. It might reboot your machine or reformat the hard disk: it could do anything: that's what undefined behaviour means.

    Fix the problem, don't try to patch it over. Your desire not to provide a copy ctor is unreasonable. Do a deep copy, not a shallow one; implement a reference counted smart pointer if you don't want a deep copy, but do something positive to address the problem, rather than rely on behaviour that is intrinsically unreliable.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  9. #9
    Join Date
    Dec 2003
    Location
    India
    Posts
    6
    Hi,

    I am trying my best to trace the actual problem. Out of curosity
    i just wanna experiment that. But the code above is still
    throwing runtime error dialog box...that means it's not going
    into the catch block.

    Thanx and Regards
    Ram.

  10. #10
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Originally posted by Ram_Gupta
    Hi,

    I am trying my best to trace the actual problem. Out of curosity
    i just wanna experiment that. But the code above is still
    throwing runtime error dialog box...that means it's not going
    into the catch block.

    Thanx and Regards
    Ram.
    Right. You're catching the wrong type of exception. I didn't indicate which "type" of exception to catch, because of the reasons Graham stated. You asked if something is possible. Yes, it is possible. Don't rely on it. Fix the problem "the right way" - Graham offered several suggestions. Abandon the path you seem to be headed down.
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  11. #11
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    void main()??? That speaks levels...

    What complier are you using and what platform are you running under. Regardless if the question/behaviour is undefined as indicated.

  12. #12
    Join Date
    Dec 2003
    Location
    India
    Posts
    6
    Hi,

    Can u tell me which exception i should catch?

    Thankx and Regards
    Ram

  13. #13
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471
    You shouldn't be catching any exceptions, you should be fixing the underlying problem!
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  14. #14
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by Ram_Gupta
    Hi,

    Can u tell me which exception i should catch?

    Thankx and Regards
    Ram
    As others have indicated and I'll say so too, the behaviour is undefined, but under WinDoze it would be a SException presumably of type access violation, if one occurred, and that's a crap shoot on where in your prog you might encounter one, since your freeing memory that may be in use somewhere else in your program.

    There is a couple of underlying issues.

    1. whatever complier your using should catch this, and issue a warning about using an uinit'd var.
    2. The debug build of your particular memory management routines should catch this also.
    3. it's undefined because it all depends I can't imagine 1 and 2 not occuring

    so your code should never be getting into the field with this type of error.

  15. #15
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Aside from fully agreeing with the previous posters, you can catch Windows SEH exceptions with
    catch(...)
    but you don't know what hit you.
    All the buzzt
    CornedBee

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center