boost::python意外的发行器行为

boost::python意外的发行器行为,python,c++,python-2.7,boost,destructor,Python,C++,Python 2.7,Boost,Destructor,我正在创建一个Boo::Python < /Cord>包,用Python访问C++结构。 一切正常,但是通过boost::python调用析构函数的行为非常烦人。 不知道boost::python模块或python是否有问题 Python版本2.7 我正在做的是 struct World { void set(const std::string &msg) { this->msg = msg; } const std::string &greet() con

我正在创建一个Boo::Python < /Cord>包,用Python访问C++结构。 一切正常,但是通过
boost::python
调用析构函数的行为非常烦人。 不知道
boost::python
模块或python是否有问题

Python版本2.7

我正在做的是

struct World
{
    void set(const std::string &msg) { this->msg = msg; }
    const std::string &greet() const { return msg; }
    std::string msg;
    World() {cout<< "CONSTRUCTOR : WORLD" << endl; }
    ~World() { cout<< "DESTRUCTOR : WORLD " << msg << endl; }
};

struct WorldData
{
    WorldData() { std::cout<< "WorldData : CONSTRUCTOR " << std::endl; my_data.clear(); }
    ~WorldData() { std::cout<< "WorldData : DESTRUCTOR " << std::endl; my_data.clear(); }
    void append_data(const World &data) { my_data.insert(my_data.end(), data); std::cout << "Data Appended\n"; }
    const vector<World> &get_data() const { return my_data; }
    vector<World> my_data;
};
输出:

LOOP START
Data Appended
DESTRUCTOR : WORLD howdaadfay : 0 // Why this destructor called
Data Appended
DESTRUCTOR : WORLD howdaadfay : 0 // Why this destructor called
DESTRUCTOR : WORLD howdaadfay : 1 // Why this destructor called
Data Appended
Data Appended
DESTRUCTOR : WORLD howdaadfay : 3 // This destructor called when variable goes out of scope
LOOP CLOSED
WorldData : DESTRUCTOR 
DESTRUCTOR : WORLD howdaadfay : 0
DESTRUCTOR : WORLD howdaadfay : 1
DESTRUCTOR : WORLD howdaadfay : 2
DESTRUCTOR : WORLD howdaadfay : 3
这就是析构函数的问题所在。 他们为什么被叫,谁叫这些破坏者。
但是如果我用C++代码做同样的事情。一切正常。当变量超出范围时调用所有析构函数。

可能启动python库阻止编译器执行复制省略?为什么我们看不到构造函数消息呢?我对同样的事情感到非常惊讶。如果构造函数创建了对象的副本,为什么不能打印构造函数消息:(为什么编译器会这么做??为什么这个对象的析构函数被调用两次?为什么没有构造函数调用调用这些析构函数?为什么你根本没有调用构造函数?这真的很奇怪。构造函数输出没有Python是否工作?如果没有,我用C++做了同样的代码,没有集成。python,那么它就很好了。我还在寻找解决方案。我也做了很多随机案例,但仍然无法理解这种行为。
import libboopyclass

def my_module_function(input):
    print 'input : ', input
    print 'Python : Module function called'

    world_data = libboopyclass.WorldData()

    p = libboopyclass.World()
    print 'LOOP START'

    for i in range(0, 4):
        p.set("howdaadfay : " + str(i))

        world_data.append_data(p)

    del p
    print 'LOOP CLOSED'

    return world_data
LOOP START
Data Appended
DESTRUCTOR : WORLD howdaadfay : 0 // Why this destructor called
Data Appended
DESTRUCTOR : WORLD howdaadfay : 0 // Why this destructor called
DESTRUCTOR : WORLD howdaadfay : 1 // Why this destructor called
Data Appended
Data Appended
DESTRUCTOR : WORLD howdaadfay : 3 // This destructor called when variable goes out of scope
LOOP CLOSED
WorldData : DESTRUCTOR 
DESTRUCTOR : WORLD howdaadfay : 0
DESTRUCTOR : WORLD howdaadfay : 1
DESTRUCTOR : WORLD howdaadfay : 2
DESTRUCTOR : WORLD howdaadfay : 3