473,395 Members | 2,795 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,395 software developers and data experts.

Cannot dynamic_cast the same object for the second time?

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

Sep 20 '05 #1
7 2882
wa*********@gmail.com wrote:
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.

--
Salu2
Sep 20 '05 #2
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?

Sep 20 '05 #3

wa*********@gmail.com wrote:
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

Sep 20 '05 #4
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?

Sep 20 '05 #5
wa*********@gmail.com wrote:
I have found a workaround.

A workaround to what?


Brian
Sep 20 '05 #6
wa*********@gmail.com wrote:
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

Sep 21 '05 #7

wa*********@gmail.com wrote:
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

Sep 21 '05 #8

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

Similar topics

17
by: steve | last post by:
I am writing for game application. Performance is an issue. Any advise would be appreiciated.
13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when...
8
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid. Invalid casts of pointers give a '0'-result. As...
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
2
by: hijinks | last post by:
I am trying to port a python program into c++ to more learn about c++... so i am trying to do a MVC design.. so here is my events class Event { public: Event() {} Event(string text) {...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
8
by: pietromas | last post by:
In the example below, why does the dynamic_cast fail (return NULL)? It should be able to cast between sibling classes ... #include <iostream> class A { public: virtual const int get()...
15
by: Bo Yang | last post by:
Hi, I can understand static_cast, reinterpret_cast and const_cast, they all work at compile time. But I can figure out how the C++'s dynamic- cast works? Could you please explain how for me?...
25
by: lovecreatesbea... | last post by:
Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.