Boost logo

Boost Users :

Subject: Re: [Boost-users] [Memory] Problems when calling virtual class function
From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2010-07-12 08:42:05


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net