473,788 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

prevent delete again

dear all,
i am using vc++ 6.0 sp5
i have a class called Address,it allocated some memory space for streetname
and city
i first define it as

Address add = new Address("Madria n","UK");
...
delete add // delete it explicitly

end of program
but my program get error that before the end of program the destructor is
called again and try to delete the street again,
how to prevent action, i have tried to use sizeof (street) or !=null to
check in if loop, but it still can jump and try to delete street again
it is not i want, please give me solution.
here is my destructor
Address::~Addre ss () {
cout << "address destructor is called"<<endl;
if (sizeof(street) >0 )
{
cout <<"Delete Street"<<endl;
delete [] street;
street = NULL;
}

if (city !=NULL)
{
cout <<"Delete city"<<endl;
delete [] city;
city = NULL;
}
}
Jul 22 '05 #1
3 1816


penny336 wrote:
dear all,
i am using vc++ 6.0 sp5
i have a class called Address,it allocated some memory space for streetname
and city
i first define it as

Address add = new Address("Madria n","UK");
..
delete add // delete it explicitly

end of program
but my program get error that before the end of program the destructor is
called again and try to delete the street again,
how to prevent action, i have tried to use sizeof (street) or !=null to
check in if loop, but it still can jump and try to delete street again
it is not i want, please give me solution.
here is my destructor
Address::~Addre ss () {
cout << "address destructor is called"<<endl;
if (sizeof(street) >0 )
{
cout <<"Delete Street"<<endl;
delete [] street;
street = NULL;
}

if (city !=NULL)
{
cout <<"Delete city"<<endl;
delete [] city;
city = NULL;
}
}

sizeof(street) will return the size of the pointer - not the size of the
memory it points to. just do it like you did for city - check if it's
null. actually you con't need the check since a delete on NULL has no
effect. Of yourse you always need to set the pointer to NULL when you
deleted it!
Even easier would be to use std::string instead of doing the
memory-handling yourself...

Jul 22 '05 #2

"penny336" <pe******@hotma il.com> wrote in message
news:c2******** **@www.csis.hku .hk...
dear all,
i am using vc++ 6.0 sp5
i have a class called Address,it allocated some memory space for streetname and city
i first define it as

Address add = new Address("Madria n","UK");
..
delete add // delete it explicitly

end of program
but my program get error that before the end of program the destructor is
called again and try to delete the street again,
how to prevent action, i have tried to use sizeof (street) or !=null to
check in if loop, but it still can jump and try to delete street again
it is not i want, please give me solution.
here is my destructor
Address::~Addre ss () {
cout << "address destructor is called"<<endl;
if (sizeof(street) >0 )
{
cout <<"Delete Street"<<endl;
delete [] street;
street = NULL;
}

if (city !=NULL)
{
cout <<"Delete city"<<endl;
delete [] city;
city = NULL;
}
}


Neither of those will work. When your object is destructed its gone, there's
no point setting street and city to NULL since the object is about to
disappear.

Since you didn't post a complete program its hard to say what it wrong with
your code, but I suspect that what you have done wrong is fail to define a
copy constructor and assignment operator for your class, does that ring any
bells? Get out your favourite C++ book and read why a class with a
destructor must also have a copy constructor and assignment operator.

john
Jul 22 '05 #3
penny336 wrote:
Address add = new Address("Madria n","UK");
..
delete add // delete it explicitly

end of program
but my program get error that before the end of program the destructor is
called again and...


Welcome to C++. That works fine in Java, but not in C++. In
fact, that's not even valid C++ code.

In C++, if you want an "automatic" variable, don't do the 'new'
thing. Just write it like:

Address add("Madrian", "UK");

....Then, you don't ever need to delete this object, because it is
"automatica lly" cleaned up. The destructor is called when the
variable goes out of scope, that is, when it hits the closest
enclosing close brace.

If you want to manage the memory yourself, then you need to keep
a pointer to what you're allocating:

Address *add = new...
// ...
delete add;

See the asterisk in there? That star means, "this thing is a
pointer. I'm going to control allocation and deallocation
myself."

If neither of these things help you out, post the actual code
that you're working on. Rather, post the smallest subset of the
code that compiles and runs and doesn't do what you want. (In
other words, strip it down so that JUST the behavior you're
talking about is happening.)

-tom!
Jul 22 '05 #4

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

Similar topics

4
1857
by: Dean R. Henderson | last post by:
Hi, I am getting HTTP/1.1 500 Internal Server Errors when I try to create a new ASP.NET Web Service or ASP.NET Web Application. I have been able to create these in the past and my previous ones can still be opened, so I suspect I have done something to mess up my security configuration or settings. I am using Visual Studio 2003 on a Windows 2000 Server running IIS 5. Thanks for any help to resolve this problem.
4
5027
by: D. Shane Fowlkes | last post by:
This may be a very simple question but I'm stumped none the less. I have a form where a user provides comments. There's a Grid below this form which displays all comments in the table so far. On the Page_Load sub, this grid is loaded. When they submit the form, a SaveComments Sub will insert the record into a table and then disable the button and textbox (greyed out) and display a message in a asp:label saying "thank you...". The last...
10
5567
by: mttc | last post by:
I read articles that suggest preventing delete by throwing Exception from RowDeleting Event. I not understand where I can catch this Error?
5
1512
by: Maria Anthonsen | last post by:
I have filled a datagrid with data from a dataset. The dataset was filled with a dataadapter - and I used the wizard to create insert, update, delete commands. I would like to prevent the user from adding a new record in the datagrid, but still be able to change/delete data - is this possible?? Maria
18
3397
by: Gleep | last post by:
I've searched google intensely on this topic and it seems noone really knows how to approch this. The goal I don't want clients to give out their usernames and passwords to friends, since the site relies on subscrption fees. Sessions ID's are matched between the browser and the server. So a users can login with same username and password and those sessions are tracked individually. Some suggest create table fields with the session ID...
3
2297
by: Ryan Liu | last post by:
Can someone give a sample to prevent a row from being deleted in a datatable? I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not work. I need verify if there other data using data in this row before actually remove it from datagrid. I can certainly control with Delete button. But if I want to allow the user to use Del key on the keyboard, I lost this kind control.
1
5812
by: Andy | last post by:
My application is written in .NET (C#) with the inline Edit mode from DataGrid. When a SAVE button is pressed it will perform a action on the database either to add, edit or delete data that a user has entered. When the refresh button is pressed at the top of the page the same exact data is re-submitted again resulting duplicate transaction in the database. Is there anyway of stopping this happening? From client-side script, how can we...
4
1653
by: eighthman11 | last post by:
this is probably very simple but I don't know. Is there a way to prevent two people from signing on to an Access Application using the some login information???? I can't have two people on the application at the same time with the same user name. I use currentuser for some locking and filtering. Thanks for any help. ray
2
7354
by: ruzaika | last post by:
Hey all, I have a table name JobDescription. In it i have my PK as JobId, which I have set as a auto increment. I created a form based on that table. I have disable record navigators and record selectors. From my switchboard when i click new job, it open the form i have created. And populates the JobId with a new id.
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
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...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
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...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
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.