- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Ubuntu 中使用 SWIG 2.0.10 来调用 Java 中的 C++ 代码。
我的 C++ 代码是:
//ImgPro.h:
#include <vector>
typedef struct _bin
{
char* name;
float value;
} Bin;
typedef struct imgprops
{
std::vector<Bin> color;
int width;
int height;
char *print;
} ImageProperties;
class ImgPro
{
public:
ImgPro();
ImageProperties *processImage(char* imagePath);
};
processImage函数定义为:
ImageProperties* ImgPro::processImage(char *imagePath)
{
ImageProperties* imgProp = new ImageProperties();
imgProp->width = 200;
imgProp->height = 200;
char* fp = new char(5);
strcpy(fp, "abc!");
imgProp->print = fp;
Bin outputBin1;
char *name1 = new char(strlen("red")+1);
strcpy(name1, "red");
outputBin1.name = name1;
outputBin1.value = 0.125;
Bin outputBin2;
char *name2 = new char(strlen("blue")+1);
strcpy(name2, "blue");
outputBin2.name = name1;
outputBin2.value = 0.27;
vector<Bin> tempVec;
tempVec.push_back(outputBin1);
tempVec.push_back(outputBin2);
imgProp->color = tempVec;
return imgProp;
因此,为了使用 swig 生成 jni 代码,我使用了以下 swig 文件(注意:vector.i 文件是使用此 example 创建的):
%module CBIR
// to handle char** has String_Array in Java
%include <various.i>
%include "vector.i"
%{
#include "ImgPro.h"
%}
// to handle char** has String_Array in Java
%apply char **STRING_ARRAY { char ** };
// memory release
%extend imgprops {
~imgprops(){
if($self != NULL)
{
// releasing print element
if($self->print != NULL)
delete[] $self->print;
// releasing vector elements
for(uint x = 0; x < $self->color.size(); x++)
{
Bin currentBin = $self->color[x];
if(currentBin.name != NULL)
delete[] currentBin.name;
}
// releasing stuct Pointer
delete $self;
}
}
}
%include "ImgPro.h"
%template(BinVec) std::vector<Bin>;
这会在 swig_wrap 文件中生成下一个函数:
SWIGINTERN void delete_imgprops(imgprops *self){
if(self != NULL)
{
// releasing print element
if(self->print != NULL)
delete[] self->print;
// releasing vector elements
for(uint x = 0; x < self->color.size(); x++)
{
Bin currentBin = self->color[x];
if(currentBin.name != NULL)
delete[] currentBin.name;
}
// releasing stuct Pointer
delete self;
}
}
在 delete ImageProperties c++ 函数中调用。
但是,在 Java 中运行以下代码永远不会释放在 C++ 中分配的内存(调用函数 delete_imgprops):
ImgPro imgObject = new ImgPro();
ImageProperties propObject = imgObject.processImage("imagem123-jpg");
int width = propObject.getWidth();
int height = propObject.getHeight();
String fingerPrint = propObject.getPrint();
propObject.delete();
imgObject.delete();
于是,通过分析代码流程,找到了内存没有释放的原因。 SWIG 生成的 ImageProperties.Java 文件包含删除功能等:
public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
CBIRJNI.delete_ImageProperties(swigCPtr);
}
swigCPtr = 0;
}
}
行“CBIRJNI.delete_ImageProperties(swigCPtr);”永远不会被调用,因为 var swigCMemOwn 始终为假。
我了解到,因为 Java 端不分配内存,所以它也不释放它,那么我该怎么做才能确保 java 释放内存而不对 swig 生成的 java 文件进行任何修改?
我发现释放内存的解决方案是在 delete() 函数上注释 if(swigCMemOwn) 测试,但我认为这不是最好的方法!
谢谢,塞尔吉奥
最佳答案
你可以看看
%newobject
带有工厂方法的指令(在 swig 2.0 中)。它告诉 swig,一个特定的函数将返回一个新对象,并且 java 代理类也应该负责清理 c++ 内存。生成的代码会将 swigCMemOwn 设置为 true,从而导致调用 c++ 析构函数。
如果您确实自己调用了 delete 方法,那很好 - 您只需改变您的编程风格,将 swig'ed 对象视为类似于文件句柄或数据库连接的东西。完成后,您会在这些对象上调用 close(),因为您不想等待 Java 的 GC 在以后某个未知的时间点启动并收集这种昂贵的资源 - 您希望手动管理它。
但是您显然还必须记住遵守良好的代码纪律,以确保在调用 delete() 后不会在任何地方使用 java 对象
关于java - SWIG java : releasing memory allocated in c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21068884/
N3485 20.6.9.1 [allocator.members]/1 说: Calls to these functions that allocate or deallocate a parti
我想编写一个调用 createHook() 的自定义分配器在对象构造和对称之后 destroyHook()就在对象销毁之前。我以这种方式使用我的分配器: class Object {}; class
我正在用 C++ 重新创建一个链表,并且在重载 += 运算符时得到了一个错误的指针。我想我只是以错误的方式使用了分配器,但我可能是错的。 这里是上下文: void MyLinkedList::oper
Allocator concept和 std::allocator_traits没有说明 allocate 是否会抛出。 所以当我使用分配器编写容器时,如何知道是检查返回类型还是使用 catch? 最
C++20 删除了 construct()和 destruct()成员(member)来自 std::allocator .我应该如何构造通过 std::allocator::allocate() 分
这个问题听起来可能相当初级,但这是我与另一位合作开发人员的辩论。 我注意在可能的地方分配堆栈,而不是堆分配它们。他在和我说话并看着我的肩膀并评论说没有必要,因为他们在表现方面是一样的。 我一直认为堆栈
这个问题听起来可能相当初级,但这是我与另一位合作开发者的争论。 我一直在尽可能地堆栈分配东西,而不是堆分配它们。他一边跟我说话,一边看着我,并评论说没有必要,因为它们在性能方面是相同的。 我一直认为堆
在 Java 程序中,当需要分配数千个相似大小的对象时,最好(在我看来)有一个“池”(这是一个单一的分配),其中包含可以从中提取的保留项目需要的时候。这个单一的大分配不会像数千个较小的分配那样使堆碎片
我正在尝试使用 TBB 来提升使用 OpenCV 的计算机视觉项目的性能。这是代码中给出访问冲突的部分。 #include #include "opencv2/objdetect/objdetect
我对一个问题有疑问,特别是关于 this 的问题回答。 有一部分留给读者作为练习(这本身不是问题),特别是 Jonathan Wakely(答案的作者)说: This code asserts tha
Allocator concept和 std::allocator_traits不要说当分配失败时 allocate 会做什么——它会返回 nullptr 还是抛出异常? 当我使用标准分配器 API
我有充分的理由不应该做这样的事情吗?示例: 我有一个类(class)MyClass。在那里我有这个实现: - (id)copyWithZone:(NSZone*)zone { MyClass
相关但不重复:请参阅此答案的底部,在单击此问题下方的“关闭”按钮之前,我解决了您可能想要声明的重复项。 自动生成 ROS (Robot Operating System) message C++ 头文
据我所知std::allocator::construct在旧版本的 C++ 上仅需要两个参数;第一个是指向原始的、未构造的内存的指针,我们要在其中构造 T 类型的对象。第二个是用于初始化该对象的元素
40个不同的分配函数给40个不同的调用点 void f00(size_t sz) { void* ptr = malloc(sz); free(ptr); } void f01(size_t sz)
我在使用 RenderScript 时一直遇到内存管理问题,所以我认为由于 Allocation.createFromBitmap()/createTyped() 消耗内存,Allocation.de
我正在尝试使用 valgrind 跟踪段错误。我从 valgrind 收到以下消息: ==3683== Conditional jump or move depends on uninitialise
实际上,我正在尝试创建一个包含 n 个多媒体文件(包括图像和视频)的应用程序。我的应用程序大小约为 34MB,我的 Assets 大小约为 60mb。当我在普通设备上加载应用程序时,我们没有遇到任何问
STL 容器有一个模板参数可以选择自定义分配器。花了一段时间,但我想我明白它是如何工作的。不知何故,它并不是很好,因为给定的分配器类型没有直接使用,而是反弹到另一种类型的分配器。我终于可以使用它了。
new int[0]在 C++ 中是允许的,但 std::allocator().allocate(0)定义好? 更一般地说,所有分配器都必须接受 0作为参数分配? 编辑: 阅读答案后,我测试了 Vi
我是一名优秀的程序员,十分优秀!