[Memory] Problems when calling virtual class function
Hello, I am doing a research about Monte Carlo Tree Search efficiency on KBNK chess ending. I use framework and algorithm implementation in C++. Since I had some memory leaks, that I couldn't manage to identify, I decided to try Boost's shared_ptr. I reorganized the whole thing to use it, but I encountered a problem, which I cannot solve. The problem occurs, when I try to call class member function, which was declared "virtual" in super class. If I try to call functions, that are not redefined, everything works as expected. Ex.: class Base{ public: int val = 0; virtual void fun(){ /* do something */} }; class Derived : public Base{ public: void fun(){ /*redefined*/} void fun1(){ /*not redefined*/} }; /* ... */ boost::shared_ptr<Base> object(new Derived); object->val = 1; //this works ok object->fun1(); //also works ok object->fun(); //does not work, I get segfault and "<symbol is not available>" error in stack trace I get the same problem if I use boost::shared_ptr<Derived> instead of boost::shared_ptr<Base>. If I change fun() in Base class to non-virtual thing works (then I cannot use polymorphism of course). I use Boost 1.4 on Ubuntu 10.04. Hope someone could help me. JAnez --- Janez Urevc Pod anteno 3a 6320 Portorož Slovenija GSM: +386 (0)40 731 632 email: janez@janezek.org blog: http://www.janezek.org skype: slashrsm gtalk: janez@janezek.org twitter: slashrsm
On Jul 11, 2010, at 12:17 PM, Janez Urevc wrote:
Hello,
I am doing a research about Monte Carlo Tree Search efficiency on KBNK chess ending. I use framework and algorithm implementation in C++. Since I had some memory leaks, that I couldn't manage to identify, I decided to try Boost's shared_ptr. I reorganized the whole thing to use it, but I encountered a problem, which I cannot solve.
The problem occurs, when I try to call class member function, which was declared "virtual" in super class. If I try to call functions, that are not redefined, everything works as expected. Ex.:
class Base{ public: int val = 0; virtual void fun(){ /* do something */} };
class Derived : public Base{ public: void fun(){ /*redefined*/} void fun1(){ /*not redefined*/} };
If you declare "fun" as virtual in the base class, it needs to be virtual in the derived class, too. Many compilers will warn you about this. -- Marshall
On 11 July 2010 21:28, Marshall Clow
On Jul 11, 2010, at 12:17 PM, Janez Urevc wrote:
Hello,
I am doing a research about Monte Carlo Tree Search efficiency on KBNK chess ending. I use framework and algorithm implementation in C++. Since I had some memory leaks, that I couldn't manage to identify, I decided to try Boost's shared_ptr. I reorganized the whole thing to use it, but I encountered a problem, which I cannot solve.
The problem occurs, when I try to call class member function, which was declared "virtual" in super class. If I try to call functions, that are not redefined, everything works as expected. Ex.:
class Base{ public: int val = 0; virtual void fun(){ /* do something */} };
class Derived : public Base{ public: void fun(){ /*redefined*/} void fun1(){ /*not redefined*/} };
If you declare "fun" as virtual in the base class, it needs to be virtual in the derived class, too. Many compilers will warn you about this.
-- Marshall
Unfortunately does not fix my problem. I will fix it anyway, of course. :) JAnez
On 7/11/2010 2:28 PM, Marshall Clow wrote:
If you declare "fun" as virtual in the base class, it needs to be virtual in the derived class, too.
No, it absolutely does not. It will be virtual, whether you put the keyword there or not.
Many compilers will warn you about this.
That's why it's a warning, not an error.
Janez Urevc wrote:
Hello,
I am doing a research about Monte Carlo Tree Search efficiency on KBNK chess ending. I use framework and algorithm implementation in C++. Since I had some memory leaks, that I couldn't manage to identify, I decided to try Boost's shared_ptr. I reorganized the whole thing to use it, but I encountered a problem, which I cannot solve.
The problem occurs, when I try to call class member function, which was declared "virtual" in super class. If I try to call functions, that are not redefined, everything works as expected. Ex.:
class Base{ public: int val = 0; virtual void fun(){ /* do something */} };
class Derived : public Base{ public: void fun(){ /*redefined*/} void fun1(){ /*not redefined*/} };
/* ... */ boost::shared_ptr<Base> object(new Derived); object->val = 1; //this works ok object->fun1(); //also works ok object->fun(); //does not work, I get segfault and "<symbol is not available>" error in stack trace
I get the same problem if I use boost::shared_ptr<Derived> instead of boost::shared_ptr<Base>. If I change fun() in Base class to non-virtual thing works (then I cannot use polymorphism of course).
I use Boost 1.4 on Ubuntu 10.04.
Works fine for me on MSVC8, after fixing the obvious 'int val = 0;'. This is a very common usage of shared_ptr, the error must be in the code you haven't shown. Jeff
participants (4)
-
Eric J. Holtman
-
Janez Urevc
-
Jeff Flinn
-
Marshall Clow