B4Alpha-Aft3r0mega / javacpp

Automatically exported from code.google.com/p/javacpp
GNU General Public License v2.0
0 stars 0 forks source link

Core dupmp with callback FunctionPointer #30

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
On version 0.3 of JavaCpp I get core dump on android when I try to add callback 
function using Manager.AddEventListener. Here is my native cpp code:

Manger.h:
  namespace Private {
    calss Manager {
      public:
        typedef void (*pnfEvent_t)(Event const* event, void* pointer);
        bool addEventListener(pnfEvent_t event, void* pointer);
    }
  }

Event.h
  Namespace Private {
    class Event {
      public:
        uint8 GetEventId() { return _id; };
    }
  }

which I translated to java:

Manager.java
  @Namespace("Private")
  @Platform(include = {"Manager.h", "Event.h"})
  public class Manager extends Pointer {
    static { load(); }
    @Name("pnfEvent_t")
    public static class OnEvent extends FunctionPointer {
      static { load(); }
      public OnEvent() { allocate(); }
      public OnEvent(Pointer p) { super(p); }
      protected final native void allocate();
      public native void cal(@Const Event, Pointer pointer);
    }
    public native @Cast("bool") boolean AddEventListener(OnEvent event, Pointer pointer);
  }

Event.java
  @Namespace("Private")
  @Platform(include = {"Event.h"})
  public class Event extends Pointer {
    static { load(); }
    public native @Cast("uint8") int GetEventId();
  }

This code works fine on 0.2 and I didn't change anything after updating javacpp 
version. The only thing that I've tried changing was bridge code generation 
with and without "-header" option, but this didn't change anything.

Any hints what could be wrong here?

Original issue reported on code.google.com by Dariusz.Luksza on 11 Dec 2012 at 8:40

GoogleCodeExporter commented 8 years ago
I fixed one more thing with named FunctionPointer:
http://code.google.com/p/javacpp/source/detail?r=342553da4575bf89494cbf8de713a7b
8da76b46e
Does it still crash with that update? And does it crash on normal Java SE too?

Original comment by samuel.a...@gmail.com on 12 Dec 2012 at 2:11

GoogleCodeExporter commented 8 years ago
No it doesn't fix this. When I try to run it on Java SE I got:

libjniManager.so: undefined symbol: _ZN9Private7Manager10s_instanceE

Original comment by Dariusz.Luksza on 13 Dec 2012 at 8:45

GoogleCodeExporter commented 8 years ago
Hum, I haven't tested nested FunctionPointer classes, although I agree it 
should work.

Original comment by samuel.a...@gmail.com on 15 Dec 2012 at 1:23

GoogleCodeExporter commented 8 years ago
Let's see, your sample code contains a lot errors. I fixed it to the following

namespace Private {
    class Event {
    public:
        unsigned char GetEventId() { return 42; };
    };
    class Manager {
    public:
        typedef void (*pnfEvent_t)(Event const* event, void* pointer);
        bool addEventListener(pnfEvent_t event, void* pointer) { return true; }
    };
}

@Namespace("Private")
@Platform(include = "Foo.h")
public class Foo {
    static { Loader.load(); }
    public static class Event extends Pointer {
        static { Loader.load(); }
        public native @Cast("unsigned char") byte GetEventId();
    }
    public static class Manager extends Pointer {
        public Manager() { allocate(); }
        private native void allocate();
        @Name("pnfEvent_t")
        public static class OnEvent extends FunctionPointer {
            static { Loader.load(); }
            public OnEvent() { allocate(); }
            public OnEvent(Pointer p) { super(p); }
            private native void allocate();
            public native void call(@Const Event event, Pointer pointer);
        }
        public native @Cast("bool") boolean addEventListener(OnEvent event, Pointer pointer);
    }
    public static void main(String[] args) {
        Manager m = new Manager();
        Manager.OnEvent e = new Manager.OnEvent();
        m.addEventListener(e, null);
    }
}

But nothing bad happens when I run
$ javac -cp javacpp.jar Foo.java
$ java -jar javacpp.jar Foo
$ java  -cp javacpp.jar Foo

So, there is no problem on Java SE? Can you provide some code that crashes on 
Android?

Original comment by samuel.a...@gmail.com on 15 Dec 2012 at 1:42

GoogleCodeExporter commented 8 years ago
Unfortunate my native Manager implementation uses "singleton pattern" and do 
not define public constructors. This is third party API designed in a way that 
you need to call Manger::Create() first an then you can obtain single instance 
using Manager::Get().

Original comment by Dariusz.Luksza on 16 Dec 2012 at 10:01

GoogleCodeExporter commented 8 years ago
btw. if I don't create objects on java side using 'new' keyword should I also 
define public constructors and allocate() methods for such objects?

In this example instance of event will come from native code therefore I didn't 
declare constructors and allocate() method.

Original comment by Dariusz.Luksza on 16 Dec 2012 at 10:04

GoogleCodeExporter commented 8 years ago
No, we don't need to, and I don't think it's related to the issue at hand.

I am only looking for some sample code that actually crashes and that I can run 
here. If you can produce some other examples that works with JavaCV 0.2, but 
crashes with 0.3, that's fine too.

Original comment by samuel.a...@gmail.com on 17 Dec 2012 at 3:18

GoogleCodeExporter commented 8 years ago
I use JavaCPP on my small startup POC project and for now debugging this issue 
seams to be much hassle for me, therefore will stay on 0.2. If my POC will work 
out then for suser will try upgrade to current version of JavaCPP.

Original comment by Dariusz.Luksza on 29 Dec 2012 at 1:54

GoogleCodeExporter commented 8 years ago
I see, that's alright. BTW, @Name("pnfEvent_t") doesn't actually do anything 
with JavaCV 0.2, so you should remove it. With JavaCV 0.3, maybe this is 
causing some naming conflicts, so you should also try to remove it. Let me know 
when you have some time to test that! Thanks

Original comment by samuel.a...@gmail.com on 30 Dec 2012 at 6:27

GoogleCodeExporter commented 8 years ago
I can confirm that removing @Name("pnfEvent_t") didn't brake anything in 0.2 
but also didn't change anything in 0.3. So the problem still exists.

Original comment by Dariusz.Luksza on 17 Feb 2013 at 5:16

GoogleCodeExporter commented 8 years ago
yet another hint, it looks like 'env->FindClass' cannot find nested class. 
During my investigations call env->FindClass("Manager$OnEvent")' in 
JavaCPP_getClass always returns NULL.

Any hints?

Original comment by Dariusz.Luksza on 17 Feb 2013 at 6:57

GoogleCodeExporter commented 8 years ago
Another hint here is that Manager.OnEvent can be called from different thread 
and this was working in 0.2

Original comment by Dariusz.Luksza on 17 Feb 2013 at 8:50

GoogleCodeExporter commented 8 years ago
Oh, yes, "Foo$Manager$OnEvent" would need to get preloaded to work on Android. 
I didn't think of updating that part when making changes to the callback code. 
That's why it worked with Java SE, but not Android. This update should fix this:
http://code.google.com/p/javacpp/source/browse/src/main/java/com/googlecode/java
cpp/Generator.java#1432
Let me know, and thanks for looking into this!

Original comment by samuel.a...@gmail.com on 24 Feb 2013 at 2:42

GoogleCodeExporter commented 8 years ago
I can confirm that 0.3-10-ga41cb1d works on Android. Thanks for fixing this!

btw. do you plan to do release in nearest future? maybe something like 0.3.1 or 
0.4?

Original comment by Dariusz.Luksza on 24 Feb 2013 at 2:53

GoogleCodeExporter commented 8 years ago
Next weekend, if all goes well...

Original comment by samuel.a...@gmail.com on 24 Feb 2013 at 2:54

GoogleCodeExporter commented 8 years ago
Ok, it's released! Enjoy

Original comment by samuel.a...@gmail.com on 3 Mar 2013 at 11:56