程序退出时 JNA 错误

发布于 2024-11-03 20:06:58 字数 1282 浏览 3 评论 0原文

有以下内容

__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;
}

所以我在 c++和 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()) );
}

上面将打印出字符串列表。到目前为止,一切都很好。但是在 main 返回之后(在最终确定期间?),我收到了一个错误

The instruction at "%08X" referenced memory at "%08x". The memory could not be "read".

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

有人可以告诉我我做错了什么吗?或者至少为我们提供更多资源?

So I have the following in 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;
}

and in 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()) );
}

The above will print out a list of Strings. So far so good. But after main returns (during finalization?) I get an error along the lines of

The instruction at "%08X" referenced memory at "%08x". The memory could not be "read".

I get the same error when trying to manually free() the char** or each individual char*

Can someone tell me what I've done wrong? Or at least point we to some more resources?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时常饿 2024-11-10 20:06:58

这个:

 *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 的频率!

This:

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

should be:

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

And I just noticed that:

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

should be:

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

which shows how often I use malloc these days!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文