Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
freeing the memory using delete for a class object created using new gives exception.
The class is
C++
class CSnp : public CObject
{
    public:
        CSnp();
        ~CSnp();
        char liststr[50];
        char sSnpDesc[50];
        CTime tDateTime;
        int iDCSSerialKey;        
 };
Posted
Updated 23-Aug-11 2:05am
v2
Comments
CPallini 23-Aug-11 7:57am    
Please dont cross-post. And please provide implementation code.
Philippe Mori 23-Aug-11 8:00am    
Uses code block for you code and provide useful code if you want any help.

an exception on delete normally means you've exceeded the bounds of the allocation during use ... i'd look at the use of
C++
liststr 
and
C++
sSnpDesc 
at runtime
 
Share this answer
 
Put a breakpoint in your destructor. The main reason to get an exception is that the destrcutor is called twice because of wrong code.
 
Share this answer
 
The problem is not in your class definition, it is in the code that uses it.

Although... since you inherit CSnp from CObject you should make your destructor virtual, unless you are absolutely sure that no class will ever inherit from CSnp! (but that is most likely not the cause of your problem)

An exception during delete may be caused by

1. the implementation of CSnp::~CSnp() (e. g. by calling virtual functions or accessing other data that's already destroyed)

2. calling delete[] when delete was called for or vice versa

3. repeated call of the destructor, e. g. because somewhere it's called directly (this might happen in case 2 above, or when your object(s) is stored in some container that takes special care of destruction and you do so as well)

4. plenty of other causes that are pretty hard to determine without using the debugger (in other words, if it's none of the above, use the debugger!)
 
Share this answer
 
Comments
manoharbalu 24-Aug-11 0:43am    
Pl. provide me links with any debuggers and how to trap these errors with the debugger as I dont have one.
Stefan_Lang 24-Aug-11 3:58am    
Most compilers come with a debugger or provide an IDE (integrated development environment) that has one built-in. Check your documentation if that is the case. Otherwise you can pick up the GNU debugger, 'gdb', at http://www.gnu.org/s/gdb/

If you have Visual C++ (you can get a free version from MS, and your class derives from CObject which happens to coincide with a base class from MicroSofts MFC), this program comes with a built-in debugger! In that case check the online help on how to set break points and other functions of the debugger. (clicking on the left side of the editor window next to the line and just outside the window should do it already; otherwise the shortcut Ctrl+B will open a dialog to set a breakpoint)

The main and most basic thing to do with a debugger is setting a 'breakpoint' at any executable line in your source code to make the program pause execution at that point, and then examine the values stored in your variables. Check your debuggers documentation to learn how to do that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900