Fix some memory leaks in Db

This commit is contained in:
Jordan Bancino 2022-11-18 19:36:15 +00:00
parent f6851de75f
commit e7ad166877
3 changed files with 19 additions and 5 deletions

View file

@ -14,7 +14,8 @@ Due: January 1, 2023
[~] Data abstraction layer [~] Data abstraction layer
[ ] Database upgrades/migration path [ ] Database upgrades/migration path
[ ] Caching and cache control [x] Caching
[ ] Cache eviction
[x] Make memory info access O(1) [x] Make memory info access O(1)
[x] Make config option 'id' optional; print warning if not present started as root. [x] Make config option 'id' optional; print warning if not present started as root.
[x] Write install and uninstall scripts [x] Write install and uninstall scripts

View file

@ -330,8 +330,6 @@ DbLock(Db * db, char *prefix, char *key)
ref->json = json; ref->json = json;
ref->ts = diskTs; ref->ts = diskTs;
ref->size = DbComputeSize(ref->json); ref->size = DbComputeSize(ref->json);
ref->prefix = UtilStringDuplicate(prefix);
ref->key = UtilStringDuplicate(key);
} }
} }
else else
@ -365,8 +363,8 @@ DbLock(Db * db, char *prefix, char *key)
pthread_mutex_init(&ref->lock, NULL); pthread_mutex_init(&ref->lock, NULL);
ref->ts = UtilServerTs(); ref->ts = UtilServerTs();
ref->size = DbComputeSize(ref->json); ref->size = DbComputeSize(ref->json);
ref->prefix = prefix; ref->prefix = UtilStringDuplicate(prefix);
ref->key = key; ref->key = UtilStringDuplicate(key);
/* If cache is enabled, cache this ref */ /* If cache is enabled, cache this ref */
if (db->cache) if (db->cache)
@ -389,6 +387,7 @@ finish:
Free(file); Free(file);
Free(hash); Free(hash);
return ref; return ref;
} }
@ -407,8 +406,11 @@ DbUnlock(Db * db, DbRef * ref)
file = DbFileName(db, ref->prefix, ref->key); file = DbFileName(db, ref->prefix, ref->key);
fp = fopen(file, "w"); fp = fopen(file, "w");
Free(file);
if (!fp) if (!fp)
{ {
pthread_mutex_unlock(&db->lock);
return 0; return 0;
} }
@ -427,7 +429,15 @@ DbUnlock(Db * db, DbRef * ref)
pthread_mutex_destroy(&ref->lock); pthread_mutex_destroy(&ref->lock);
Free(ref); Free(ref);
} }
else
{
/* This ref should be in the cache, just update it's size */
db->cacheSize -= ref->size;
ref->size = DbComputeSize(ref->json);
db->cacheSize += ref->size;
}
pthread_mutex_unlock(&db->lock);
return 1; return 1;
} }

View file

@ -151,6 +151,9 @@ extern char *
extern JsonValue * extern JsonValue *
JsonValueInteger(long integer); JsonValueInteger(long integer);
extern long
JsonValueAsInteger(JsonValue *);
extern JsonValue * extern JsonValue *
JsonValueFloat(double floating); JsonValueFloat(double floating);