Cannot dynamic_cast the same object for the second time?

W

wangtianthu

I am writing a program using some RPC infrastructure. there is code
like this:

MyServer* server = dynamic_cast<MyServer*>(GetServerByName(MY_SERVER));

Here GetServerByName() will return a GenericServer*, which is the
parent class of MyServer*. The problem here is, I can only do this for
the first time. After some server cycle and I reach here again, there
will be a SIGSEGV problem on dynamic_cast. I am sure that the returned
value has correct dynamic type, since it works for the first time and
the address returned by GetServerByName() is also not changed.

What might be the cause here? I know there are too many thing behind
this process that may complicate this problem, but I just want to know,
in what case will dynamic_cast give you memory error?

Tian
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Here GetServerByName() will return a GenericServer*, which is the
parent class of MyServer*. The problem here is, I can only do this for
the first time. After some server cycle and I reach here again, there
will be a SIGSEGV problem on dynamic_cast. I am sure that the returned
value has correct dynamic type, since it works for the first time and
the address returned by GetServerByName() is also not changed.

Don't be sure without testing it.
 
W

wangtianthu

Yeah, I have debugged it stepwise and checked the memory address
returned by GetServerByName(), it's not changed, nor does the pointed
object deleted. In fact it's GetServerByName() only accesses a STL
vector that has everything inside it, and it has not been changed
between two calls. Any idea?
 
G

Greg

I am writing a program using some RPC infrastructure. there is code
like this:

MyServer* server = dynamic_cast<MyServer*>(GetServerByName(MY_SERVER));

Here GetServerByName() will return a GenericServer*, which is the
parent class of MyServer*. The problem here is, I can only do this for
the first time. After some server cycle and I reach here again, there
will be a SIGSEGV problem on dynamic_cast. I am sure that the returned
value has correct dynamic type, since it works for the first time and
the address returned by GetServerByName() is also not changed.

What might be the cause here? I know there are too many thing behind
this process that may complicate this problem, but I just want to know,
in what case will dynamic_cast give you memory error?

Tian

I'll make two guesses: The object at that address has been deleted. Or
its container has been has been resized causing its held memory to be
reallocated.

Greg
 
W

wangtianthu

I have found a workaround. I made the server pointer which stores the
object static, so we avoid dynamic_cast'ing it from the container
again. Surprisingly it work. So I think the object should not have been
deleted nor moved due to the container change. However, I don't like
this workaround. Is there any other guesses? Greg?
 
G

Greg

I have found a workaround. I made the server pointer which stores the
object static, so we avoid dynamic_cast'ing it from the container
again. Surprisingly it work. So I think the object should not have been
deleted nor moved due to the container change. However, I don't like
this workaround. Is there any other guesses? Greg?

If dynamic_cast crashes when passed a pointer for the second time,
thenn that pointer is no longer referencing a valid object. The
workaround only defers the problem. The pointer is just as invalid when
stored in a static variable as it was when passed to dynamic_cast. The
pointer should not be stored unless it is valid.

There are only two ways in which the pointer to the object could have
become invalid: the object's memory could have been freed (by deleting
the object) or the contents of memory occupied by the object could have
been trashed (by code that writes beyond the allocated bounds of an
array, for example). The memory trashing theory is the less likely
cause, while the object being deleted is the more likely explanation.
Only by stepping through all the intervening instructions can one or
the other explanation be found to be the right one in this case.

Greg
 
P

peter.koch.larsen

I have found a workaround. I made the server pointer which stores the
object static, so we avoid dynamic_cast'ing it from the container
again. Surprisingly it work. So I think the object should not have been
deleted nor moved due to the container change. However, I don't like
this workaround. Is there any other guesses? Greg?
From your description the problem could be that you destroy some vital
part of the object - likely the vptr or the memory pointed to by vptr.

Check these areas in your debugger (you might have to do a little
investigation to discover where the different parts are located, but it
should not be to difficult).
Then find out who's overwriting that memory.
Then fix.

/Peter
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top