Boost logo

Boost Users :

Subject: [Boost-users] [Memory] Problems when calling virtual class function
From: Janez Urevc (janez_at_[hidden])
Date: 2010-07-11 15:17:22


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_at_[hidden]
blog: http://www.janezek.org
skype: slashrsm
gtalk: janez_at_[hidden]
twitter: slashrsm

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