Archived

This forum has been archived. Please start a new discussion on GitHub.

a problem with dynamicCast

when i using the IceInternal::Handle seens a problem:

there is a shared lib:

// testlib.h
namespace XPP
{

class TestIceBase1 : public Ice::Object
{
int data_;
};
class TestIceBase2
{
int data_;
};

class TestIceObject1 : //virtual public TestBase ,
virtual public Ice::Object
{

};

typedef IceInternal::Handle<TestIceObject1> TestIceObject1Ptr;

class TestIceObject2 : virtual public TestIceBase1
{

};

typedef IceInternal::Handle<TestIceObject2> TestIceObject2Ptr;

TestIceBase1 * g_ice_object1;
TestIceBase1 * g_ice_object2;
TestXPPBase1 * g_XPP_object1;
TestXPPBase1 * g_XPP_object2;

void init_object();

}

// testlib.cpp

#include "testlib.h"

using namespace XPP;

void XPP::init_object()
{
g_ice_object1 = new TestIceObject1;
g_ice_object2 = new TestIceObject2;
g_XPP_object1 = new TestXPPObject1;
g_XPP_object2 = new TestXPPObject2;
}


then there is a exe program:
// test.cpp
int main( char ** argv , int argc )
{
XPP::init_object();

// test ice object1
{
printf("g_ice_object1: ");
// XPP::TestIceObject1Ptr testobj = XPP::TestIceObject1Ptr::dynamicCast( XPP::g_ice_object1 );
XPP::TestIceObject1 * testobj = dynamic_cast<XPP::TestIceObject1 *>( XPP::g_ice_object1.get() );
if( testobj ) {
printf("ok\n");
} else {
printf("failed\n");
}
}

// test ice object1
{
printf("g_ice_object2: ");
XPP::TestIceObject2Ptr testobj = XPP::TestIceObject2Ptr::dynamicCast( XPP::g_ice_object2 );
if( testobj ) {
printf("ok\n");
} else {
printf("failed\n");
}
}
}


after run the test program, the result always failed, can someone can tell me what is the problem is?

Comments

  • matthew
    matthew NL, Canada
    You are not supposed to directly use anything in the IceInternal namespace. If you want a reference counted class and handle you should use IceUtil::Handle and IceUtil::Shared (or SimpleShared if the target is not supposed to be used concurrently).
  • IceUtil::Handle also incorrent

    i already change the code like this:

    class TestIceBase1 : public Ice::Object
    {
    int data_;
    };
    class TestIceBase2
    {
    int data_;
    };

    class TestIceObject1 : //virtual public TestBase ,
    virtual public Ice::Object
    {

    };

    typedef IceUtil::Handle<TestIceObject1> TestIceObject1Ptr;

    class TestIceObject2 : virtual public TestIceBase1
    {

    };

    typedef IceUtil::Handle<TestIceObject2> TestIceObject2Ptr;

    but there still cann't dynamicCast ,
    just return 0

    :mad:
  • matthew
    matthew NL, Canada
    I'm sorry, I don't understand what you are trying to do. The shared library code you posted originally can't possibly work .
    XPP::TestIceObject1 * testobj = dynamic_cast<XPP::TestIceObject1 *>( XPP::g_ice_object1.get() );
    

    g_ice_object1 is a pointer, not a handle, so you cannot call get.

    To get prompt assistance I suggest that you post something that does not use IceInternal types, is self-contained and compilable and demonstrates your problem and then we can help fix it. It also helps to know the version of Ice you are using, your compiler and finally your platform.
  • solved

    i think there is must my mistake, not the ICE.

    :)
  • The original post had something to do with dynamic casting while using a shared library. I had a similar problem and took me a bit to get to the bottom of it. I'm posting a note which may be of help to others.

    The following combination of factors requires special attention:
    1. under Linux
    2. using shared libraries which are dynamicaly loaded (with dlopen)
    3. performing dynamic casting: any dynamic casting is affected, basic std::dynamic_cast and Ice's ObjectPrx::dynamicCast().

    Read about it here:
    GCC Frequently Asked Questions - GNU Project - Free Software Foundation (FSF)

    a somewhat related post is here:
    http://www.zeroc.com/forums/bug-reports/1319-dlopen-linux.html#post5556