Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIGSEGV on std::vector<Person>* persons #217

Closed
louxiu opened this issue Dec 9, 2017 · 6 comments
Closed

SIGSEGV on std::vector<Person>* persons #217

louxiu opened this issue Dec 9, 2017 · 6 comments
Labels

Comments

@louxiu
Copy link
Contributor

louxiu commented Dec 9, 2017

Come for help again. 😭
hello.h is the interface. hello_impl.h and hello_impl.cc is the implement. The SIGSEGV happens on hello_impl.cc#SayHelloTo. I test the libhello.a in C++ and it is ok. I doubt that the problem is that the jvm has GCed the reference, but I can't solve it.

// hello.h
#ifndef HELLO_H_
#define HELLO_H_

#include <string>
#include <vector>

namespace louxiu {
struct Person {
  std::string name;
};

class HelloCallback {
 public:
  virtual void OnReply() = 0;
};

class Hello {

 public:

  static Hello* Create();
  virtual void SayHelloTo(std::vector<Person>* persons) = 0;

 protected:
  virtual ~Hello() {
  }
};
}

#endif /* HELLO_H_ */
// hello_impl.h
#ifndef HELLO_IMPL_H_
#define HELLO_IMPL_H_

#include "hello.h"

namespace louxiu {

class HelloImpl : public Hello {

public:

  HelloImpl();
  ~HelloImpl();

  virtual void SayHelloTo(std::vector<Person>* persons) override;
};

}

#endif /* HELLO_IMPL_H_ */
// hello_impl.cc
#include "hello_impl.h"
#include <iostream>

namespace louxiu {

Hello* Hello::Create() {
  return new HelloImpl();
}

HelloImpl::HelloImpl() {

}

HelloImpl::~HelloImpl() {

}

void HelloImpl::SayHelloTo(std::vector<Person>* persons) {
  for (auto it = persons->begin(); it != persons->end(); it++) {
    std::cout << "hello1: " << (*it).name << std::endl; // <------  SIGSEGV on (*it).name
  }
}

}
@Properties(target = "org.bytedeco.javacpp.libhello", value = {@Platform(include = "<hello.h>", link = "hello", library = "hello")})
public class libhello implements InfoMapper {

    public void map(InfoMap infoMap) {
        infoMap.put(new Info("louxiu::HelloCallback").virtualize())
                .put(new Info("std::vector<louxiu::Person>").pointerTypes("PersonVector").define())
                ;
    }
}
    public static void main(String[] args) {
        System.setProperty("java.library.path",
                System.getProperty("java.library.path") + File.pathSeparator
                        + "/Users/louxiu/projects/javacpp-presets/libhello/target/classes/org/bytedeco/javacpp/macosx-x86_64/");

        libhello.Hello hello = libhello.Hello.Create();
        libhello.Person person = new libhello.Person();
        BytePointer name = new BytePointer("123");
        person.name(name);

        libhello.PersonVector pointPersonVector = new libhello.PersonVector();
        pointPersonVector.put(person);
        hello.SayHelloTo(pointPersonVector);
    }
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00000001299e1460, pid=73243, tid=0x0000000000001903
#
# JRE version: Java(TM) SE Runtime Environment (8.0_102-b14) (build 1.8.0_102-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.102-b14 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# C  [libhello.dylib+0x7460]  louxiu::HelloImpl::SayHelloTo(std::__1::vector<louxiu::Person, std::__1::allocator<louxiu::Person> >*)+0x40
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/louxiu/projects/javacpp-test/hs_err_pid73243.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
hello1: 
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
@saudet
Copy link
Member

saudet commented Dec 9, 2017 via email

@louxiu
Copy link
Contributor Author

louxiu commented Dec 9, 2017

Tried it without luck. Maybe there is another problem?

        hello.SayHelloTo(pointPersonVector);

        name.deallocate();
        person.deallocate();
        pointPersonVector.deallocate();

@saudet
Copy link
Member

saudet commented Dec 9, 2017 via email

@louxiu
Copy link
Contributor Author

louxiu commented Dec 9, 2017

Wow, I see. 😃 Thanks!!!
IMHO, this is error prone. Can we improve this?

This works.

libhello.PersonVector pointPersonVector = new libhello.PersonVector(1);
pointPersonVector.put(0, person);

This doesn't work.

libhello.PersonVector pointPersonVector = new libhello.PersonVector();
pointPersonVector.put(0, person);

@saudet
Copy link
Member

saudet commented Dec 9, 2017 via email

@saudet
Copy link
Member

saudet commented Dec 14, 2017

There, I fixed that in the commit above. We can now do new PersonVector(person) or new PersonVector().put(person) and it works as expected. Thanks for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants