map elements being deleted

W

will551

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)
 
M

mlimber

will551 said:
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)

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
 
W

will551

mlimber said:
will551 said:
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)

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
 
M

mlimber

will551 said:
mlimber said:
will551 said:
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)

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/how-to-post.html#faq-5.8) that we can
copy and pasted into our editors unchanged to demonstrate the problem?

Cheers! --M
 
W

will551

Can you post a minimal but complete sample (cf.
http://parashift.com/c++-faq-lite/how-to-post.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
 
W

will551

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top