Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 5th, 2006, 04:15 PM
will551
Guest
 
Posts: n/a
Default 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

  #2  
Old December 5th, 2006, 04:35 PM
mlimber
Guest
 
Posts: n/a
Default 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

  #3  
Old December 5th, 2006, 05:05 PM
will551
Guest
 
Posts: n/a
Default 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

  #4  
Old December 5th, 2006, 05:05 PM
mlimber
Guest
 
Posts: n/a
Default 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

  #5  
Old December 5th, 2006, 05:25 PM
will551
Guest
 
Posts: n/a
Default Re: map elements being deleted

Quote:
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

  #6  
Old December 6th, 2006, 10:15 AM
will551
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles