473,795 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete Problem

I have the following line of code:

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

Below is MyData:
SomeDataStructu re *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 2936
* Venn Syii:
I have the following line of code:

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


Below is MyData:
SomeDataStructu re *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:
SomeDataStructu re *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:
SomeDataStruct ure *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:
SomeDataStructu re *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:
SomeDataStruc ture *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*******@hotm ail.com> wrote in message
news:SN******** *********@twist er.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
912
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 nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
2
8849
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 to a "never ending" statement. We found out, that vacuuming table B after delete did the trick. It seems to us the database has to do scan thru deleted records on B while
3
13755
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. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
2
2064
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 example, the delete operation has no effect: var z = <abc><foo a='1'>1</foo><foo a='2'>2</foo></abc>; alert(z.foo.(@a == '2')); delete z.foo.(@a == '2');
6
1476
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 pObject and assigned to m_pObj which is a CGLObj pointer variable in the CGLTest class. CGLTest::~CGLTest() //destructor { if(m_pObj) {
6
12470
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 delete operator. Let me use an example to illustrate this: /********************************************************/ #include <new> #include <iostream>
5
3113
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 the row. The Delete method marks the row as Deleted in the DataSet or DataTable but does not remove it. Instead when the DataAdapter encounters a row marked as Deleted, it executes its DeleteCommand method to delete the row at the data source. The...
9
8179
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); mmFree(&local_Head,(char *)mem); CPRMemory::SetMemoryHeadAs(local_Head,head_type); } ///////////////////// void* operator new(size_t sz, int head_Type) {
4
4865
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 sql DataAdapter (da) da.SelectCommand.CommandText = "Select * from Servertbl1" da.Fill(ds, "tbl1") so far, so good. If I add a row to the datagridview I use the following sqlDataAdapter code to update the server table - which works OK when...
11
4081
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, CrtdUser and Date And Edit and Delete buttons
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.