fix memory leak in JNI getHistory()

This commit is contained in:
Simon Morlat 2016-05-10 16:02:11 +02:00
parent 23e33b3380
commit fa00efddbb
4 changed files with 21 additions and 7 deletions

View file

@ -3727,10 +3727,12 @@ extern "C" jobjectArray _LinphoneChatRoomImpl_getHistory(JNIEnv* env, jobject th
env->SetObjectArrayElement(jHistory, i, jmsg);
env->DeleteLocalRef(jmsg);
}
history = history->next;
}
ms_list_free(list);
/*getChatMessage() acquired a ref that is "transfered" to the java object. We must drop
* the reference given by linphone_chat_room_get_history_range()*/
ms_list_free_with_data(list, (void (*)(void*))linphone_chat_message_unref);
return jHistory;
}
extern "C" jobjectArray Java_org_linphone_core_LinphoneChatRoomImpl_getHistoryRange(JNIEnv* env

View file

@ -193,13 +193,12 @@ static int create_chat_message(void *data, int argc, char **argv, char **colName
if(atoi(argv[3])==LinphoneChatMessageIncoming){
new_message->dir=LinphoneChatMessageIncoming;
linphone_chat_message_set_from(new_message,linphone_chat_room_get_peer_address(cr));
linphone_chat_message_set_to(new_message,local_addr);
new_message->to = local_addr; /*direct assignation to avoid a copy*/
} else {
new_message->dir=LinphoneChatMessageOutgoing;
linphone_chat_message_set_from(new_message,local_addr);
new_message->from = local_addr; /*direct assignation to avoid a copy*/
linphone_chat_message_set_to(new_message,linphone_chat_room_get_peer_address(cr));
}
linphone_address_destroy(local_addr);
new_message->time = (time_t)atol(argv[9]);
new_message->is_read=atoi(argv[6]);

View file

@ -234,5 +234,12 @@ public interface LinphoneChatMessage {
* @throw LinphoneCoreExeption .
*/
void putChar(long character) throws LinphoneCoreException;
/**
* Frees the underlying native resource of the message.
* It should not be accessed afterwards.
* This is for optimizing the memory resources at runtime. Not calling this does not result in a memory leak.
**/
void destroy();
}

View file

@ -3,7 +3,7 @@ package org.linphone.core;
import java.io.UnsupportedEncodingException;
public class LinphoneChatMessageImpl implements LinphoneChatMessage {
protected final long nativePtr;
protected long nativePtr;
private native byte[] getText(long ptr);
private native long getPeerAddress(long ptr);
private native String getExternalBodyUrl(long ptr);
@ -112,7 +112,7 @@ public class LinphoneChatMessageImpl implements LinphoneChatMessage {
return new ErrorInfoImpl(getErrorInfo(nativePtr));
}
protected void finalize() throws Throwable{
unref(nativePtr);
destroy();
super.finalize();
}
@ -160,4 +160,10 @@ public class LinphoneChatMessageImpl implements LinphoneChatMessage {
public void putChar(long character) throws LinphoneCoreException {
putChar(nativePtr, character);
}
public void destroy(){
if (nativePtr != 0) {
unref(nativePtr);
nativePtr = 0;
}
}
}