Menu

#972 Wrong in another module, called the destructor

closed-invalid
python (260)
5
2008-12-21
2008-12-15
Yoonsik Oh
No

There are two modules to the class of the same name, called the destructor of other modules.

== a.i ==
%module A
%{
#include <iostream>
struct test {
~test() {
std::cout << "A.~test()" << std::endl;
}
};
%}

struct test {};

== b.i ==
%module B
%{
#include <iostream>
struct test {
~test() {
std::cout << "B.~test()" << std::endl;
}
};
%}

struct test {};

== test.py ==
import A
import B
v = A.test()

$ swig -python -c++ a.i
$ g++ -shared a_wrap.cxx -o _A.so -I/usr/include/python2.5/
$ swig -python -c++ b.i
$ g++ -shared b_wrap.cxx -o _B.so -I/usr/include/python2.5/
$ python test.py

Expected Results:
A.~test()

But, Actual results:
B.~test() <-- called wrong destructor

It is always called, importing the future of the module.

Discussion

  • William Fulton

    William Fulton - 2008-12-21

    This is expected behaviour as you have explicitly provided two different implementations of one class. Use a .h and possibly a .cpp file instead for the implementation so that you have just one implementation. Then %include the appropriate .h file.

     
  • William Fulton

    William Fulton - 2008-12-21
    • assigned_to: nobody --> wsfulton
    • status: open --> closed-invalid
     
  • Yoonsik Oh

    Yoonsik Oh - 2008-12-22

    No, This is just an example to explain this, This is intentionally made.

    A and B is the same as the name is different class in different module.

    In fact, I use the following code, my program had abnormal termination uncommonly.

    == a.i ==
    %module A
    %{
    #include <vector>
    void testA(const std::vector<int>& v) {}
    %}

    %include "std_vector.i"
    %template(VectorIntA) std::vector<int>;
    void testA(const std::vector<int>& v);

    == b.i ==
    %module B
    %{
    #include <vector>
    void testB(const std::vector<int>& v) {}
    %}

    %include "std_vector.i"
    %template(VectorIntB) std::vector<int>;
    void testB(const std::vector<int>& v);

    Because VectorIntA use destructor of VectorIntB.

    There is a lot of program using several modules.

    In this case, how can I do?

     
  • William Fulton

    William Fulton - 2008-12-22

    You've got two different proxy classes for your vector ... wrap it just once. Either:

    Put just one of the %template declarations in one of the modules and %import that module by the 2nd module, or create a third module for your vector %template declaration and %import that by both modules A and B.

     
  • Yoonsik Oh

    Yoonsik Oh - 2008-12-22

    I did not familiar with the use of import.

    Thanks a lot!

     

Log in to post a comment.