accessing virtual method in shared libs.

Anitha Boyapati anithab@sankhya.com
Fri Dec 8 12:28:00 GMT 2006


Hi,

 I am trying to access a method from 'child' class in a shared library 
 which is declared as virtual in its 'base' class.

 I get a segmentation fault.Is this some sort of limitation ?
 (Please note : the library is libchild made from child.cc
  only and base::test()  is virtual)
----------------------------------------------------------------
 Please see the following code :

#file : base.cc

namespace XYZ {
class base {
 public:
 base() {}
        virtual void test()
        	{
        cout<<"helo from base test()"<<endl;
        	}
	};
};

	*************

#file child.cc

 class child : public base {
        public:
        child() {
        cout << "helo world!This is  child constructor."<<endl;
        }
        void test()
        {
        cout<<"hi..this from child" <<endl;
        }
        };
                                                      
                          
extern "C" child* get_type(void)     //for demangling
{				     //used in dlsym()
    return (new child());
}
	***************

#file:main.cc

 int main()
{
base *ldmem = load_libmem();       //succesfully loads libchild.cc
ldmem->test();                     //segmentation fault here
}

	**************
 >g++ -shared -o libchild.so child.cc

 ./a.out is created from base.cc and main.cc

 >./a.out
 helo world!This is  child constructor.
 Segmentation fault

 ------------------------------------------------------------

 Cant help if code for loading (dlopen and dlsym ) are not there.
 Should truncate the bigger code.

 Please let me know if i have erred somewhere!

[ B/n my gcc version is gcc version 3.2.3 on i686 ]
      
				

-- 
Regards,
Anitha B,
Sankhya Technologies Private Limited.




More information about the Gcc-help mailing list