
December 5th, 2006, 04:15 PM
| | | 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 | 
December 5th, 2006, 04:35 PM
| | | Re: map elements being deleted
will551 wrote: Quote:
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 | 
December 5th, 2006, 05:05 PM
| | | Re: map elements being deleted
mlimber wrote: Quote:
will551 wrote: Quote:
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 | 
December 5th, 2006, 05:05 PM
| | | Re: map elements being deleted
will551 wrote: Quote:
mlimber wrote: Quote:
will551 wrote: Quote:
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 | 
December 5th, 2006, 05:25 PM
| | | Re: map elements being deleted 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 | 
December 6th, 2006, 10:15 AM
| | | Re: map elements being deleted
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 |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|