程序退出时 JNA 错误
有以下内容
__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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个:
应该是:
我刚刚注意到:
应该是:
这显示了我这些天使用 malloc 的频率!
This:
should be:
And I just noticed that:
should be:
which shows how often I use malloc these days!