Cannot dynamic_cast the same object for the second time?

Discussion in 'C++' started by wangtianthu@gmail.com, Sep 20, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. 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
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Sep 20, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
  4. Greg Guest

    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
     
    Greg, Sep 20, 2005
    #4
  5. Guest

    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, 2005
    #5
  6. Default User Guest

    wrote:

    > I have found a workaround.



    A workaround to what?




    Brian
     
    Default User, Sep 20, 2005
    #6
  7. Greg Guest

    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
     
    Greg, Sep 21, 2005
    #7
  8. Guest

    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, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steve
    Replies:
    17
    Views:
    719
    Mike Smith
    Sep 13, 2004
  2. verec
    Replies:
    5
    Views:
    3,081
    verec
    Aug 16, 2005
  3. Brian Takita
    Replies:
    2
    Views:
    148
    Brian Takita
    Jul 25, 2005
  4. http://links.i6networks.com

    Why '' Is Matched First Time, Not Second Time

    http://links.i6networks.com, Aug 21, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    175
    Sherm Pendley
    Aug 22, 2004
  5. yelipolok
    Replies:
    4
    Views:
    282
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page