473,383 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Delete Problem

I have the following line of code:

if (MyData)
{
delete MyData;
MyData= NULL;
}

Below is MyData:
SomeDataStructure *MyData;

Every time this code segment is called I get a fatal error that causes my
program to terminate.

I use the debugger and upon reaching this line of code MyData is still valid
and still has the correct information in it.
I have searched the internet and pretty much figured that it was some sort
of memory allocation problem... either I'm deleting what's not there, or
even I've read deleting from different stacks? Any ideas as to why this
delete cause and error?

Jul 22 '05 #1
9 2904
* Venn Syii:
I have the following line of code:

if (MyData)
{
delete MyData;
MyData= NULL;
}
It suffices to write
delete MyData;


Below is MyData:
SomeDataStructure *MyData;

Every time this code segment is called I get a fatal error that causes my
program to terminate.

I use the debugger and upon reaching this line of code MyData is still valid
*Seems* to be valid.
and still has the correct information in it.
Any area of memory contains what it did earlier unless it has been
changed...

Any ideas as to why this delete cause and error?


Technically:

* Your pointer MyData might be invalid, i.e. already deleted.

* The object's destructor might reference invalid data or divide
by zero or whatever.

* The process might be in an invalid state (stack, memory manager,
whatever) due to earlier errors.

Design-wise:

* You're using raw pointers instead of containers and smart-pointers.

* You haven't ensured the class invariant through all operations.

* You don't have a class invariant.

--
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?
Jul 22 '05 #2
Venn Syii wrote:
I have the following line of code:

if (MyData)
{
delete MyData;
MyData= NULL;
}

Below is MyData:
SomeDataStructure *MyData;

Every time this code segment is called I get a fatal error that causes my
program to terminate.

I use the debugger and upon reaching this line of code MyData is still valid
and still has the correct information in it.
How do you know it's "valid"? On what do you base your conclusion of
its validity?
I have searched the internet and pretty much figured that it was some sort
of memory allocation problem... either I'm deleting what's not there, or
even I've read deleting from different stacks? Any ideas as to why this
delete cause and error?


The most common mistake leading to such error is deleting the memory
twice. After the first deletion the memory may still be OK (not reclaimed
by the OS) which can be misinterpreted that the pointer is still valid.

Another mistake, less common, is not allocating the memory in the first
place but using an address of a static or automatic object. Since the
memory wasn't allocated using 'new', it shouldn't be freed using 'delete'.

Read FAQ 5.8.

V
Jul 22 '05 #3
Alf P. Steinbach wrote:
* Venn Syii:
I have the following line of code:

if (MyData)
{
delete MyData;
MyData= NULL;
}

It suffices to write
delete MyData;


No, it does not. Setting a pointer to NULL after deletion prevents any
further double deletions of the same memory.
[...]


V
Jul 22 '05 #4
Venn Syii wrote:
I have the following line of code:

if (MyData)
{
delete MyData;
MyData= NULL;
}

Below is MyData:
SomeDataStructure *MyData;

Every time this code segment is called I get a fatal error that causes my
program to terminate.

I use the debugger and upon reaching this line of code MyData is still valid
and still has the correct information in it.
I have searched the internet and pretty much figured that it was some sort
of memory allocation problem... either I'm deleting what's not there, or
even I've read deleting from different stacks? Any ideas as to why this
delete cause and error?

Looks like a memory access error. Why don't you run purify/valgrind ?

Krishanu
Jul 22 '05 #5
Thanks for your fast answer. I've tracked it through the destructor and it
get's through that fine. It's right after that it goes bad. I'm assuming
more then likely it's option:
1) Your pointer MyData might be invalid, i.e. already deleted.
in conjunction with
2)*Seems* to be valid.

I'll dig through the code more to see.
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* Venn Syii:
I have the following line of code:

if (MyData)
{
delete MyData;
MyData= NULL;
}


It suffices to write
delete MyData;


Below is MyData:
SomeDataStructure *MyData;

Every time this code segment is called I get a fatal error that causes my
program to terminate.

I use the debugger and upon reaching this line of code MyData is still
valid


*Seems* to be valid.
and still has the correct information in it.


Any area of memory contains what it did earlier unless it has been
changed...

Any ideas as to why this delete cause and error?


Technically:

* Your pointer MyData might be invalid, i.e. already deleted.

* The object's destructor might reference invalid data or divide
by zero or whatever.

* The process might be in an invalid state (stack, memory manager,
whatever) due to earlier errors.

Design-wise:

* You're using raw pointers instead of containers and smart-pointers.

* You haven't ensured the class invariant through all operations.

* You don't have a class invariant.

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

Jul 22 '05 #6
* Victor Bazarov:
Alf P. Steinbach wrote:
* Venn Syii:
I have the following line of code:

if (MyData)
{
delete MyData;
MyData= NULL;
}

It suffices to write
delete MyData;


No, it does not. Setting a pointer to NULL after deletion prevents any
further double deletions of the same memory.


Well, we could argue about that.

I've taken both sides (not simultanously, of course!) over the years.

Currently, my thinking is that _if_ setting it to NULL prevents a double
deletion, _then_ setting it to NULL most probably prevents detection of
a serious bug.

However, there can be other more valid reasons for setting a pointer to
NULL after deletion, e.g. for optimization purposes. For example,
deletions are rare, and this pointer is in the middle of an array of
pointers; MyData above would then be a reference to that array element.
Given the OP's definition of MyData I don't think that's the case here.

In short, I disagree. ;-)

--
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?
Jul 22 '05 #7
Heh... now I really feel newbie... what's purify/valgrind?
----- Original Message -----
From: "Krishanu Debnath" <in*****@domian.com>
Newsgroups: comp.lang.c++
Sent: Thursday, December 02, 2004 10:07 AM
Subject: Re: Delete Problem

Venn Syii wrote:
I have the following line of code:

if (MyData)
{
delete MyData;
MyData= NULL;
}

Below is MyData:
SomeDataStructure *MyData;

Every time this code segment is called I get a fatal error that causes my
program to terminate.

I use the debugger and upon reaching this line of code MyData is still
valid
and still has the correct information in it.
I have searched the internet and pretty much figured that it was some sort
of memory allocation problem... either I'm deleting what's not there, or
even I've read deleting from different stacks? Any ideas as to why this
delete cause and error?

Looks like a memory access error. Why don't you run purify/valgrind ?

Krishanu



Jul 22 '05 #8
Alf P. Steinbach wrote:
[...]
In short, I disagree. ;-)


Why am I not surprised?...
Jul 22 '05 #9

"Venn Syii" <ve*******@hotmail.com> wrote in message
news:SN*****************@twister.rdc-kc.rr.com...
I have the following line of code:

if (MyData)
{
delete MyData;


What was used to allocate memory for 'MyData'? If it
was 'new[]' (not 'new'), then you need to write

delete[] MyData;
-Mike
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
2
by: Cornelius Buschka | last post by:
Hi, we saw the following problem: We deleted all rows from a table B referencing table A (~500000 records). No problem, but the following try to delete all records from table A (~180000) lead...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
2
by: Bob Tinsman | last post by:
This problem shows up in Firefox 1.5.0.1 and Rhino 1.6R2. I've found that if I have an XML node expression that ends in a filter, I can't use it with the delete operator. In the following...
6
by: radishcarrot | last post by:
Hi, I am rather new to C++ and an invalid exception has occurs in my system which I have absolutely no idea as to why it happens. Please help! Thank you. I have created a method which takes in...
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
5
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
On a form - I have a datagridview which is docked to the entire form. The datagridview allows users to Delete and/or Add Rows. On the Form_Load event I Fill the datagridview source table with a...
11
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.