Skip to content

Commit

Permalink
fix(*): attempt to fix various memory leaks (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickersoft authored Feb 7, 2021
1 parent 8252ac4 commit bb33fa3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 21 deletions.
7 changes: 7 additions & 0 deletions bridge/odict-bridging-lib.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

// #define CGO_EXPORT_BRIDGE_EXISTS
// #include <stdlib.h>
import "C"

import (
"encoding/base64"
"encoding/json"
"unsafe"

odict "github.com/odict/odict/go"
)
Expand Down Expand Up @@ -54,6 +56,11 @@ func SearchDictionary(query, dict *C.char) *C.char {
return C.CString(string(b))
}

//export Free
func Free(res *C.char) {
C.free(unsafe.Pointer(res))
}

//export IndexDictionary
func IndexDictionary(dict *C.char) {
d := getDictionaryFromBuffer(dict)
Expand Down
40 changes: 34 additions & 6 deletions java/main/cpp/odict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,37 @@ Java_org_odict_Dictionary_lookup(JNIEnv *env, jobject, jstring query, jstring di
{
const char *dictionary = env->GetStringUTFChars(dict, 0);
const char *entry_term = env->GetStringUTFChars(query, 0);
return env->NewStringUTF(LookupEntry((char *)entry_term, (char *)dictionary));
char *result = LookupEntry((char *)entry_term, (char *)dictionary);
jstring str = env->NewStringUTF(result);

free(result);

return str;
}

extern "C" JNIEXPORT void JNICALL
Java_org_odict_Dictionary_index(JNIEnv *env, jobject, jstring encoded)
{
IndexDictionary((char *)env->GetStringUTFChars(encoded, 0));
char *enc = (char *)env->GetStringUTFChars(encoded, 0);
IndexDictionary(enc);
env->ReleaseStringUTFChars(encoded, enc);
}

extern "C" JNIEXPORT jstring JNICALL
Java_org_odict_Dictionary_search(JNIEnv *env, jobject, jstring query, jstring encoded)
{
const char *q = env->GetStringUTFChars(query, 0);
const char *result = SearchDictionary((char *)q, (char *)env->GetStringUTFChars(encoded, 0));
char *q = (char *)env->GetStringUTFChars(query, 0);
char *enc = (char *)env->GetStringUTFChars(encoded, 0);
const char *result = SearchDictionary(q, enc);

jstring res = env->NewStringUTF(result);

return env->NewStringUTF(result);
env->ReleaseStringUTFChars(query, q);
env->ReleaseStringUTFChars(encoded, enc);

free((char *)result);

return res;
}

extern "C" JNIEXPORT jstring JNICALL
Expand All @@ -38,20 +53,33 @@ Java_org_odict_Dictionary_read(JNIEnv *env, jobject, jstring path)
const char *dictionary_path = env->GetStringUTFChars(path, 0);
char *encoded = ReadDictionary((char *)dictionary_path);

return env->NewStringUTF(encoded);
jstring res = env->NewStringUTF(encoded);

env->ReleaseStringUTFChars(path, dictionary_path);

free(encoded);

return res;
}

extern "C" JNIEXPORT void JNICALL
Java_org_odict_Dictionary_write(JNIEnv *env, jobject, jstring xml, jstring path)
{
const char *dictionary_path = env->GetStringUTFChars(path, 0);
const char *content = env->GetStringUTFChars(xml, 0);

WriteDictionary((char *)content, (char *)dictionary_path);

env->ReleaseStringUTFChars(path, dictionary_path);
env->ReleaseStringUTFChars(xml, content);
}

extern "C" JNIEXPORT void JNICALL
Java_org_odict_Dictionary_compile(JNIEnv *env, jobject, jstring path)
{
const char *dictionary_path = env->GetStringUTFChars(path, 0);

CompileDictionary((char *)dictionary_path);

env->ReleaseStringUTFChars(path, dictionary_path);
}
7 changes: 4 additions & 3 deletions java/test/java/org/odict/TestDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void testLookup() throws Exception {
String json = dict.lookup("run");
String expected = "{\"term\":\"run\",\"etymologies\":[{\"id\":\"0\",\"usages\":{\"n\":{\"pos\":\"n\",\"definitions\":[\"Act or instance of running, of moving rapidly using the feet.\",\"Act or instance of hurrying (to or from a place) (not necessarily by foot); dash or errand, trip.\",\"A pleasure trip.\",\"Flight, instance or period of fleeing.\",\"Migration (of fish).\",\"A group of fish that migrate, or ascend a river for the purpose of spawning.\"]},\"v\":{\"pos\":\"v\",\"groups\":[{\"id\":\"\",\"description\":\"A number of verb usages\",\"definitions\":[\"(vertebrates) To move swiftly.\",\"(fluids) To flow.\",\"(nautical, of a vessel) To sail before the wind, in distinction from reaching or sailing close-hauled.\",\"(social) To carry out an activity.\",\"To extend or persist, statically or dynamically, through space or time.\",\"(transitive) To execute or carry out a plan, procedure or program.\"]}]}}}]}";

assertEquals(json, expected);
assertEquals(expected, json);
}

@Test
Expand All @@ -41,7 +41,8 @@ public void testWrite() throws Exception {
Dictionary dict = new Dictionary("test.odict");
String json = dict.lookup("hello");

assertEquals(json,
"{\"term\":\"hello\",\"etymologies\":[{\"id\":\"0\",\"usages\":{\"v\":{\"pos\":\"v\",\"definitions\":[\"hello world\"]}}}]}");
assertEquals(
"{\"term\":\"hello\",\"etymologies\":[{\"id\":\"0\",\"usages\":{\"v\":{\"pos\":\"v\",\"definitions\":[\"hello world\"]}}}]}",
json);
}
}
46 changes: 34 additions & 12 deletions python/odict.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,59 @@ def __find_library():

lib = cdll.LoadLibrary(__find_library())

lib.SearchDictionary.restype = c_char_p
lib.LookupEntry.restype = c_char_p
lib.ReadDictionary.restype = c_char_p
lib.SearchDictionary.restype = c_void_p
lib.LookupEntry.restype = c_void_p
lib.ReadDictionary.restype = c_void_p
lib.Free.argtypes = [c_void_p]
lib.Free.restype = None


class Dictionary:

def __init__(self, path, should_index=False):
self.__encoded_dict = lib.ReadDictionary(path.encode('utf-8'))
print("hello")
print(self.__encoded_dict)
p = path.encode('utf-8')
self.__encoded_dict = lib.ReadDictionary(p)

if should_index:
self.index()

def __del__(self):
lib.Free(self.__encoded_dict)

@staticmethod
def compile(path):
lib.CompileDictionary(path.encode('utf-8'))
p = path.encode('utf-8')
lib.CompileDictionary(p)

@staticmethod
def write(xml, path):
lib.WriteDictionary(xml.encode('utf-8'), path.encode('utf-8'))
try:
lib.WriteDictionary(xml.encode('utf-8'), path.encode('utf-8'))
except:
print("An exception occurred")

def __get_dictionary(self):
return cast(self.__encoded_dict, c_char_p).value

def search(self, query):
return self.__decode(lib.SearchDictionary(self.__encode(query), self.__encoded_dict))
v = lib.SearchDictionary(self.__encode(query), self.__get_dictionary())
d = self.__decode(cast(v, c_char_p).value)

lib.Free(v)

return d

def index(self):
lib.IndexDictionary(self.__encoded_dict)
lib.IndexDictionary(self.__get_dictionary())

def lookup(self, term):
print(self.__encoded_dict)
return self.__decode(lib.LookupEntry(self.__encode(term), self.__encoded_dict))
e = self.__encode(term)
v = lib.LookupEntry(e, self.__get_dictionary())
d = self.__decode(cast(v, c_char_p).value)

lib.Free(v)

return d

def __encode(self, str):
return str.encode('utf-8')
Expand Down

0 comments on commit bb33fa3

Please sign in to comment.