程序退出时JNA错误

JNA Error at Program Exit

本文关键字:错误 JNA 退出 程序      更新时间:2023-10-16

所以我在c++中有以下代码

__declspec(dllexport) extern "C"
char** get_prop_types( int* count ) {
  const vector<string>& r = prop_manager::get_prop_types();
  char **p = (char**)malloc( r.size() );
  char **ptr = p;
  for( vector<string>::const_iterator it = r.begin(); it!=r.end() ; ++it ) {
    *p = (char*)malloc( it->size() );
    strcpy(*p++,it->c_str());
  }
  *count = r.size();
  return ptr;
}

和java

public interface Arch extends Library {
    public Pointer get_prop_types( IntByReference size );
}
static Arch theLib; //initialization not shown
public static String[] getPropTypes() {
    IntByReference size = new IntByReference();
    Pointer strs = theLib.get_prop_types(size);
    //free is apparently handled for us?
    return strs.getStringArray(0, size.getValue());
}
public static void main( String[] args ) {
    System.out.println( Arrays.toString(getPropTypes()) );
}

上面的语句将打印出一个string列表。到目前为止一切顺利。但是在main返回之后(在结束期间?)在

这几行出现错误
The instruction at "%08X" referenced memory at "%08x". The memory could not be "read".

当尝试手动free() char**或每个单独的char*时,我得到相同的错误

谁能告诉我我做错了什么?或者至少给我们提供一些资源?

This:

 *p = (char*)malloc( it->size() );
应:

 *p = (char*)malloc( it->size() + 1);

我刚刚注意到:

 char **p = (char**)malloc( r.size() );
应:

 char **p = (char**)malloc( r.size() * sizeof(char *) );

显示了这些天我使用malloc的频率!