Quantcast

Java problem when SWIG'ing two directors with a protected virtual function in the baseclass

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Java problem when SWIG'ing two directors with a protected virtual function in the baseclass

Martin Klomp
I'm having a problem when SWIG'ing C++ code with two directors and a protected virtual function in the baseclass to java. The code is able to SWIG and compile, but crashes when running the Java code. Below a minimal reproduction scenario.

C++ code (derived.cpp):
class Foo {
  public:
      Foo() { std::cout << "C++: construct Foo" << std::endl; }
      ~Foo()  { std::cout << "C++: destruct Foo" << std::endl; }

  protected:    
      virtual int onInit(){ return 1; } 
};

class Bar : public Foo {
  public:
      Bar() { std::cout << "C++: construct Bar" << std::endl; }
      ~Bar()  { std::cout << "C++: destruct Bar" << std::endl; }
};

SWIG file (foobar.i):
%module(directors="1", allprotected="1") swig_foobar

%{
#include "derived.cpp"
%}

%include <std_string.i>

%feature("director") Foo;
%feature("director") Bar;

%include "derived.cpp"

Java code:
public class E extends Bar {
    public void run() {
        System.out.println("java: Run E");
        int err = onInit();
    }
}

The error occurs when calling the onInit() in E:

C  [swig_derived.dll+0x1b58]
j  derived.Foo.onInit()I+26
j  E.run()V+9
j  Test.main([Ljava/lang/String;)V+9
v  ~StubRoutines::call_stub

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  derived.swig_foobarJNI.Foo_onInitSwigExplicitFoo(JLderived/Foo;)I+0
j  derived.Foo.onInit()I+26
j  E.run()V+9
j  Test.main([Ljava/lang/String;)V+9
v  ~StubRoutines::call_stub

I'm stuggeling to find a solution for this problem. Implementing onInit() in Bar() does fix the problem but is not my preferred solution. Any help is appreciated.



------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Java problem when SWIG'ing two directors with a protected virtual function in the baseclass

William S Fulton
On 2 June 2016 at 15:55, Martin Klomp <[hidden email]> wrote:

> I'm having a problem when SWIG'ing C++ code with two directors and a
> protected virtual function in the baseclass to java. The code is able to
> SWIG and compile, but crashes when running the Java code. Below a minimal
> reproduction scenario.
>
> C++ code (derived.cpp):
> class Foo {
>   public:
>       Foo() { std::cout << "C++: construct Foo" << std::endl; }
>       ~Foo()  { std::cout << "C++: destruct Foo" << std::endl; }
>
>   protected:
>       virtual int onInit(){ return 1; }
> };
>
> class Bar : public Foo {
>   public:
>       Bar() { std::cout << "C++: construct Bar" << std::endl; }
>       ~Bar()  { std::cout << "C++: destruct Bar" << std::endl; }
> };
>
> SWIG file (foobar.i):
> %module(directors="1", allprotected="1") swig_foobar
>
> %{
> #include "derived.cpp"
> %}
>
> %include <std_string.i>
>
> %feature("director") Foo;
> %feature("director") Bar;
>
> %include "derived.cpp"
>
> Java code:
> public class E extends Bar {
>     public void run() {
>         System.out.println("java: Run E");
>         int err = onInit();
>     }
> }
>
> The error occurs when calling the onInit() in E:
>
> C  [swig_derived.dll+0x1b58]
> j  derived.Foo.onInit()I+26
> j  E.run()V+9
> j  Test.main([Ljava/lang/String;)V+9
> v  ~StubRoutines::call_stub
>
> Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
> j  derived.swig_foobarJNI.Foo_onInitSwigExplicitFoo(JLderived/Foo;)I+0
> j  derived.Foo.onInit()I+26
> j  E.run()V+9
> j  Test.main([Ljava/lang/String;)V+9
> v  ~StubRoutines::call_stub
>
> I'm stuggeling to find a solution for this problem. Implementing onInit() in
> Bar() does fix the problem but is not my preferred solution. Any help is
> appreciated.

I think this is a known limitation/bug for director protected methods.
The only solutions I know of are to provide the Bar::onInit in C++ or
make onInit public. Or if you can think of a fix, then a patch would
be welcome. I think there is some complexity in accessing protected
methods from the generated code that might be challenging to solve.

William



On 2 June 2016 at 15:55, Martin Klomp <[hidden email]> wrote:

> I'm having a problem when SWIG'ing C++ code with two directors and a
> protected virtual function in the baseclass to java. The code is able to
> SWIG and compile, but crashes when running the Java code. Below a minimal
> reproduction scenario.
>
> C++ code (derived.cpp):
> class Foo {
>   public:
>       Foo() { std::cout << "C++: construct Foo" << std::endl; }
>       ~Foo()  { std::cout << "C++: destruct Foo" << std::endl; }
>
>   protected:
>       virtual int onInit(){ return 1; }
> };
>
> class Bar : public Foo {
>   public:
>       Bar() { std::cout << "C++: construct Bar" << std::endl; }
>       ~Bar()  { std::cout << "C++: destruct Bar" << std::endl; }
> };
>
> SWIG file (foobar.i):
> %module(directors="1", allprotected="1") swig_foobar
>
> %{
> #include "derived.cpp"
> %}
>
> %include <std_string.i>
>
> %feature("director") Foo;
> %feature("director") Bar;
>
> %include "derived.cpp"
>
> Java code:
> public class E extends Bar {
>     public void run() {
>         System.out.println("java: Run E");
>         int err = onInit();
>     }
> }
>
> The error occurs when calling the onInit() in E:
>
> C  [swig_derived.dll+0x1b58]
> j  derived.Foo.onInit()I+26
> j  E.run()V+9
> j  Test.main([Ljava/lang/String;)V+9
> v  ~StubRoutines::call_stub
>
> Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
> j  derived.swig_foobarJNI.Foo_onInitSwigExplicitFoo(JLderived/Foo;)I+0
> j  derived.Foo.onInit()I+26
> j  E.run()V+9
> j  Test.main([Ljava/lang/String;)V+9
> v  ~StubRoutines::call_stub
>
> I'm stuggeling to find a solution for this problem. Implementing onInit() in
> Bar() does fix the problem but is not my preferred solution. Any help is
> appreciated.
>
>
>
> ------------------------------------------------------------------------------
> What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
> patterns at an interface-level. Reveals which users, apps, and protocols are
> consuming the most bandwidth. Provides multi-vendor support for NetFlow,
> J-Flow, sFlow and other flows. Make informed decisions using capacity
> planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...