Skip to content

Commit 3503fce

Browse files
committed
Fixed memory leak
1 parent 5d040ae commit 3503fce

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

JavaGUI/jni/TSDRLibraryNDK.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ tsdr_lib_t tsdr_instance;
1313

1414
struct java_context {
1515
jobject obj;
16+
jobject obj_pixels;
1617
jclass cls;
1718
jfieldID fid_pixels;
18-
jfieldID fid_width;
19-
jfieldID fid_height;
19+
int pic_width;
20+
int pic_height;
2021
jmethodID fixSize;
2122
jmethodID notifyCallbacks;
2223
jint * pixels;
@@ -97,15 +98,17 @@ void read_async(float *buf, int width, int height, void *ctx) {
9798
if ((*jvm)->GetEnv(jvm, (void **)&env, javaversion) == JNI_EDETACHED)
9899
(*jvm)->AttachCurrentThread(jvm, (void **) &env, 0);
99100

100-
jint i_width = (*env)->GetIntField(env, context->obj, context->fid_width);
101-
jint i_height = (*env)->GetIntField(env, context->obj, context->fid_height);
101+
if (context->pic_width != width || context->pic_height != height) {
102+
(*env)->DeleteLocalRef(env, context->obj_pixels);
102103

103-
if (i_width != width || i_height != height) {
104-
// fixSize(200, 200);
105104
(*env)->CallVoidMethod(env, context->obj, context->fixSize, width, height);
106105

106+
context->obj_pixels = (*env)->GetObjectField(env, context->obj, context->fid_pixels);
107+
107108
context->pixelsize = width * height;
108109
context->pixels = (jint *) realloc((void *) context->pixels, sizeof(jint) * context->pixelsize);
110+
context->pic_width = width;
111+
context->pic_height = height;
109112
}
110113

111114
jint * data = context->pixels;
@@ -118,7 +121,7 @@ void read_async(float *buf, int width, int height, void *ctx) {
118121
}
119122

120123
// release elements
121-
(*env)->SetIntArrayRegion(env, (*env)->GetObjectField(env, context->obj, context->fid_pixels), 0, context->pixelsize, context->pixels);
124+
(*env)->SetIntArrayRegion(env, context->obj_pixels, 0, context->pixelsize, context->pixels);
122125

123126
// notifyCallbacks();
124127
(*env)->CallVoidMethod(env, context->obj, context->notifyCallbacks);
@@ -132,15 +135,12 @@ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeStart (JNIEnv
132135
(*env)->DeleteLocalRef(env, obj);
133136
context->cls = (jclass) (*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, context->obj));
134137
context->fid_pixels = (*env)->GetFieldID(env, context->cls, "pixels", "[I");
135-
context->fid_width = (*env)->GetFieldID(env, context->cls, "width", "I");
136-
context->fid_height = (*env)->GetFieldID(env, context->cls, "height", "I");
137138
context->fixSize = (*env)->GetMethodID(env, context->cls, "fixSize", "(II)V");
138139
context->notifyCallbacks = (*env)->GetMethodID(env, context->cls, "notifyCallbacks", "()V");
139140

140-
jint i_width = (*env)->GetIntField(env, context->obj, context->fid_width);
141-
jint i_height = (*env)->GetIntField(env, context->obj, context->fid_height);
142-
143-
context->pixelsize = i_width * i_height;
141+
context->pic_width = 0;
142+
context->pic_height = 0;
143+
context->pixelsize = 1;
144144
context->pixels = (jint *) malloc(sizeof(jint) * context->pixelsize);
145145

146146
const char *npath = (*env)->GetStringUTFChars(env, path, 0);
@@ -152,6 +152,7 @@ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeStart (JNIEnv
152152
(*env)->ReleaseStringUTFChars(env, params, nparams);
153153
(*env)->DeleteGlobalRef(env, context->obj);
154154
(*env)->DeleteGlobalRef(env, context->cls);
155+
155156
free(context);
156157
free(context->pixels);
157158
}

JavaGUI/src/martin/tempest/core/TSDRLibrary.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
public class TSDRLibrary {
2626

2727
private BufferedImage bimage;
28-
private int[] pixels;
29-
30-
private int width;
31-
private int height;
28+
private volatile int[] pixels;
3229

3330
public enum SYNC_DIRECTION {ANY, UP, DOWN, LEFT, RIGHT};
3431

@@ -209,10 +206,12 @@ public interface FrameReadyCallback {
209206
*/
210207
private void fixSize(final int x, final int y) {
211208
if (bimage == null || bimage.getWidth() != x || bimage.getHeight() != y) {
212-
bimage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
213-
pixels = ((DataBufferInt) bimage.getRaster().getDataBuffer()).getData();
214-
width = x;
215-
height = y;
209+
try {
210+
bimage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
211+
pixels = ((DataBufferInt) bimage.getRaster().getDataBuffer()).getData();
212+
} catch (Throwable t) {
213+
t.printStackTrace();
214+
}
216215
}
217216
}
218217

@@ -221,8 +220,6 @@ private void fixSize(final int x, final int y) {
221220
* This method writes the result into the bitmap
222221
*/
223222
private void notifyCallbacks() {
224-
assert(bimage != null && pixels != null);
225-
226223
for (final FrameReadyCallback callback : callbacks) callback.onFrameReady(this, bimage);
227224
}
228225

TempestSDR/src/TSDRLibrary.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ void videodecodingthread(void * ctx) {
193193
for (i = 0; i < sizetopoll; i++)
194194
sendbuffer[i] = (screenbuffer[i] - min) / span;
195195

196+
static i = 0;
196197
context->cb(sendbuffer, width, height, context->ctx);
197198
}
198199
}
@@ -423,6 +424,7 @@ int tsdr_readasync(tsdr_lib_t * tsdr, const char * pluginfilepath, tsdr_readasyn
423424

424425
tsdr->running = 0;
425426
tsdr->nativerunning = 0;
427+
426428
return status;
427429
}
428430

0 commit comments

Comments
 (0)