473,382 Members | 1,290 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,382 software developers and data experts.

map elements being deleted

hi guys
Im having a problem with map inserts. The destructor is being called
for the element I have just added:
Here is some code:

char *recorderName = "blah";
char *mProtoRecorder = "blah";
cout << "in ::FindOrCreateRecorder..."
recorder = new TINRecorder<long long>(recorderName, mProtoRecorder);
mTheMap->insert(mTheMap::value_type(recorderName, *recorder));
printf("about to delete recorder [%p]\n, recorder);
delete recorder;
*iter = mTheMap->find(recorderName);
printf("iter address is [%p]\n", iter);
printf("iter->first address is [%p]\n", &((*iter)->first));
printf("iter->second address is [%p]\n", &((*iter)->second));
cout << "returning... " << endl;
return;

once I hit this point the destructor is called for the recorder even
though
I have already called the recorder delete. The strange thing is that
the address
of the recorder is not the address of the local variable. It is nearer
the address
of the iterator. In fact it is "a8" bytes (168) away from the start of
the iterator.

Here is the output to screen:
in ::FindOrCreateRecorder...
TINRecorder cons "this:" [14f0a68]
about to delete recorder [14f0a68]
~TINRecorder this: [14f0a68]
iter address is [ffb4f7e8]
iter->first address is [14fccb8]
iter->second address is [14fccbc]
returning...
~TINRecorder this: [ffb4f740] <--- Destructor called again here
(address a8 away from iter)
--
I wish to stop the second destructor being called.
thanks
Conor

Dec 5 '06 #1
5 1303
will551 wrote:
hi guys
Im having a problem with map inserts. The destructor is being called
for the element I have just added:
Here is some code:

char *recorderName = "blah";
char *mProtoRecorder = "blah";
cout << "in ::FindOrCreateRecorder..."
recorder = new TINRecorder<long long>(recorderName, mProtoRecorder);
mTheMap->insert(mTheMap::value_type(recorderName, *recorder));
printf("about to delete recorder [%p]\n, recorder);
delete recorder;
*iter = mTheMap->find(recorderName);
printf("iter address is [%p]\n", iter);
printf("iter->first address is [%p]\n", &((*iter)->first));
printf("iter->second address is [%p]\n", &((*iter)->second));
cout << "returning... " << endl;
return;

once I hit this point the destructor is called for the recorder even
though
I have already called the recorder delete. The strange thing is that
the address
of the recorder is not the address of the local variable. It is nearer
the address
of the iterator. In fact it is "a8" bytes (168) away from the start of
the iterator.

Here is the output to screen:
in ::FindOrCreateRecorder...
TINRecorder cons "this:" [14f0a68]
about to delete recorder [14f0a68]
~TINRecorder this: [14f0a68]
iter address is [ffb4f7e8]
iter->first address is [14fccb8]
iter->second address is [14fccbc]
returning...
~TINRecorder this: [ffb4f740] <--- Destructor called again here
(address a8 away from iter)
--
I wish to stop the second destructor being called.
You can't (ok, you can if you try hard enough, but you shouldn't). The
map has made an internal copy of your object, and when the map is
destroyed, it cleans up after itself. This is a good thing. Why do you
want to stop it?

Cheers! --M

Dec 5 '06 #2

mlimber wrote:
will551 wrote:
hi guys
Im having a problem with map inserts. The destructor is being called
for the element I have just added:
Here is some code:

char *recorderName = "blah";
char *mProtoRecorder = "blah";
cout << "in ::FindOrCreateRecorder..."
recorder = new TINRecorder<long long>(recorderName, mProtoRecorder);
mTheMap->insert(mTheMap::value_type(recorderName, *recorder));
printf("about to delete recorder [%p]\n, recorder);
delete recorder;
*iter = mTheMap->find(recorderName);
printf("iter address is [%p]\n", iter);
printf("iter->first address is [%p]\n", &((*iter)->first));
printf("iter->second address is [%p]\n", &((*iter)->second));
cout << "returning... " << endl;
return;

once I hit this point the destructor is called for the recorder even
though
I have already called the recorder delete. The strange thing is that
the address
of the recorder is not the address of the local variable. It is nearer
the address
of the iterator. In fact it is "a8" bytes (168) away from the start of
the iterator.

Here is the output to screen:
in ::FindOrCreateRecorder...
TINRecorder cons "this:" [14f0a68]
about to delete recorder [14f0a68]
~TINRecorder this: [14f0a68]
iter address is [ffb4f7e8]
iter->first address is [14fccb8]
iter->second address is [14fccbc]
returning...
~TINRecorder this: [ffb4f740] <--- Destructor called again here
(address a8 away from iter)
--
I wish to stop the second destructor being called.

You can't (ok, you can if you try hard enough, but you shouldn't). The
map has made an internal copy of your object, and when the map is
destroyed, it cleans up after itself. This is a good thing. Why do you
want to stop it?

Cheers! --M
hi M
the strange thing is that the map is global and does not get destroyed
until much later.
I have commented out some template code that is part of this code -
would this affect it?
regards
Conor

Dec 5 '06 #3
will551 wrote:
mlimber wrote:
will551 wrote:
hi guys
Im having a problem with map inserts. The destructor is being called
for the element I have just added:
Here is some code:
>
char *recorderName = "blah";
char *mProtoRecorder = "blah";
cout << "in ::FindOrCreateRecorder..."
recorder = new TINRecorder<long long>(recorderName, mProtoRecorder);
mTheMap->insert(mTheMap::value_type(recorderName, *recorder));
printf("about to delete recorder [%p]\n, recorder);
delete recorder;
*iter = mTheMap->find(recorderName);
printf("iter address is [%p]\n", iter);
printf("iter->first address is [%p]\n", &((*iter)->first));
printf("iter->second address is [%p]\n", &((*iter)->second));
cout << "returning... " << endl;
return;
>
once I hit this point the destructor is called for the recorder even
though
I have already called the recorder delete. The strange thing is that
the address
of the recorder is not the address of the local variable. It is nearer
the address
of the iterator. In fact it is "a8" bytes (168) away from the start of
the iterator.
>
Here is the output to screen:
in ::FindOrCreateRecorder...
TINRecorder cons "this:" [14f0a68]
about to delete recorder [14f0a68]
~TINRecorder this: [14f0a68]
iter address is [ffb4f7e8]
iter->first address is [14fccb8]
iter->second address is [14fccbc]
returning...
~TINRecorder this: [ffb4f740] <--- Destructor called again here
(address a8 away from iter)
--
I wish to stop the second destructor being called.
You can't (ok, you can if you try hard enough, but you shouldn't). The
map has made an internal copy of your object, and when the map is
destroyed, it cleans up after itself. This is a good thing. Why do you
want to stop it?

Cheers! --M
hi M
the strange thing is that the map is global and does not get destroyed
until much later.
I have commented out some template code that is part of this code -
would this affect it?
regards
Conor
Can you post a minimal but complete sample (cf.
http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8) that we can
copy and pasted into our editors unchanged to demonstrate the problem?

Cheers! --M

Dec 5 '06 #4
Can you post a minimal but complete sample (cf.
http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8) that we can
copy and pasted into our editors unchanged to demonstrate the problem?

Cheers! --M
It would take me a while M, I have a good few templates and different
classes.
It has been suggested that It may have something to do with
not having a copy constructor for the class being placed in the map. I
am going
to try putting in a copy constructor and see what happens and let you
know. Failing
that I will try and get a code sample together.
thanks
Conor

Dec 5 '06 #5
hi M &co.
I got the problem sorted. I put in a copy constructor which
is being called twice on the map insert line:
mTheMap->insert(mTheMap::value_type(recorderName, *recorder));
My recorder class had a pointer which needed to be recreated with
new and not given the same address as the old pointer. When
the destructor was being called, it could destruct its own.
Putting in a new into the copy constructor fixed the problem.
thanks for your time
Conor

Dec 6 '06 #6

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

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
2
by: richardkreidl | last post by:
I want to be able to delete and search for elements in a XML file, I'm using the code below for adding elements which works great: Public Sub cmdAddElement_Click(ByVal sender As System.Object,...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
4
by: Daz | last post by:
Hello everyone. I have a simple problem which can be solved quite simply, I am just going about it the wrong way. I would like to wipe every row from within a named table, but I am not too sure how...
11
by: Dennis Jones | last post by:
Hello, I would like to know if there is a known pattern for creating a map of reference-counted objects, such that when the last reference to an object is deleted, the element referring to that...
4
by: lokki | last post by:
Hello, can anybody tell me what's wrong with following example code? char *k, *v; k = new char; strcpy(k, "a2"); v = new char;
9
by: Andreas Schmitt | last post by:
I am somewhat new to C# and I ran into a problem in a small program I am writing for teaching myself. I am handling a list ob objects and I want to delete some of them inside a loop like in: ...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
1
by: Alan | last post by:
I am wondering if anyone has any better idea of how to approach this problem than I do. . . . I have a vector of items (data). I have to do a pairwise comparison of each item to each other item...
0
by: dudeja.rajat | last post by:
On Sat, Aug 30, 2008 at 2:32 PM, Fredrik Lundh <fredrik@pythonware.comwrote: Fredrik, Thanks so much. That worked. Following this, I can now see that my combo2 has no previous elements and...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.