Permalink
Browse files

- Fixed a memory leaks in the zArray implementation.

- Fixed minor warning.
- Initial usage of zGetOpt.
  • Loading branch information...
1 parent d56f541 commit 8dd600f3b599e3986a84541cb5492aa534b68457 @zanfire committed Oct 5, 2012
Showing with 61 additions and 49 deletions.
  1. +20 −22 include/zArray.h
  2. +3 −0 include/zCommon.h
  3. +1 −1 src/zConsole.cpp
  4. +1 −1 src/zEvent.cpp
  5. +1 −2 src/zFile.cpp
  6. +2 −2 src/zLogger.cpp
  7. +1 −1 src/zString.cpp
  8. +1 −1 src/zStringBuilder.cpp
  9. +2 −2 src/zTester.cpp
  10. +14 −9 tests/main.cpp
  11. +13 −6 tests/zArrayTest.cpp
  12. +2 −2 tests/zThreadTest.cpp
View
@@ -35,7 +35,7 @@ class zArray {
protected:
zMutex* _mtx;
T* _elements;
- T _default;
+
int _count;
int _size;
@@ -47,21 +47,22 @@ class zArray {
/// @param element_size_bytes is the size in bytes of each element in the class.
/// @param elements is the number of initial elements.
/// @param default value for each element in the array.
- zArray(IsThreadSafe threadSafe, int initial_size, T const& default_value) {
- init(threadSafe, initial_size, default_value);
+ zArray(IsThreadSafe threadSafe, int initial_size) {
+ init(threadSafe, initial_size);
}
/// Dtor.
virtual ~zArray(void) {
- free(_elements);
+ //free(_elements);
+ delete [] _elements;
_elements = NULL;
if (_mtx != NULL) delete _mtx;
}
/// Copy ctors
zArray(zArray<T> const& obj) {
- init((obj._mtx != NULL) ? YES : NO, obj._count , obj._default);
+ init((obj._mtx != NULL) ? YES : NO, obj._count );
copy_from(obj);
}
@@ -140,12 +141,17 @@ class zArray {
bool resize(int new_size) {
- // TODO: Implements!!!
- _elements = (T*)realloc(_elements, sizeof(T) * new_size);
-
-
- for (int i = _size; i < new_size; i++) {
- memcpy((uint8_t*)(&_elements[i]), (uint8_t*)(&_default), sizeof(T));
+ // NOTE: This way is less efficent of a realloc because it doesn't extends the current memory chunk.
+ T* tmp = new T[new_size];
+ if (tmp == NULL) {
+ return false;
+ }
+ else {
+ for (int i = 0; i < _size; i++) {
+ tmp[i] = _elements[i];
+ }
+ delete [] _elements;
+ _elements = tmp;
}
_size = new_size;
return true;
@@ -164,21 +170,13 @@ class zArray {
}
protected:
- void init(IsThreadSafe threadSafe, int initial_size, T const& default_value) {
+ void init(IsThreadSafe threadSafe, int initial_size) {
_mtx = threadSafe == YES ? new zMutex() : NULL;
if (initial_size <= 0) initial_size = 32;
_size = initial_size;
_count = 0;
- _elements = (T*) malloc(sizeof(T) * _size);
- _default = default_value;
- // Initializes
- for (int i = 0; i < _size; i++) {
- memcpy((uint8_t*)(&_elements[i]), (uint8_t*)(&_default), sizeof(T));
- }
+ _elements = new T[_size];
}
-
-
};
-#endif
-
+#endif // ZARRAY_H__
View
@@ -21,6 +21,9 @@
#if defined(_WIN32)
# include <Windows.h>
+// Disable "unreferenced formal parameter"
+# pragma warning( disable : 4100)
+
// Fast trick to cross-compile between Linux-Win32.
# define snprintf _snprintf_s
View
@@ -11,7 +11,7 @@ zConsole::~zConsole(void) {}
void zConsole::setColor(FgColor fg) {
#if defined(WIN32)
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (int)(fg) | FOREGROUND_INTENSITY);
+ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)(fg) | FOREGROUND_INTENSITY);
#else
printf("%c[%d;%d;%dm", 0x1B, BRIGHT, (int)fg, 4);
#endif
View
@@ -26,7 +26,7 @@ zEvent::~zEvent(void) {
void zEvent::wait(int timeoutMillis) {
#if defined(_WIN32)
- DWORD res = WaitForSingleObject( _event, timeoutMillis);
+ WaitForSingleObject( _event, timeoutMillis);
#else
int success = 0;
if (timeoutMillis < 0) {
View
@@ -106,8 +106,6 @@ void zFile::close(void) {
zString zFile::get_current_directory(void) {
char buf[1024];
- // Get string with a local buffer.
- char* path = NULL;
#if defined(WIN32)
// Remarks: The GetCurrentDirectroy should not used in a multi-threaded program.
int result = GetCurrentDirectoryA(sizeof(buf), buf);
@@ -128,6 +126,7 @@ zString zFile::get_current_directory(void) {
return zString(buf, sizeof(char) * result);
}
#else
+ char* path = NULL;
char* path = getcwd(buf, sizeof(buf));
int buff_size = 1024 * 64;
while (path == NULL && errno == ERANGE) {
View
@@ -6,9 +6,9 @@
#include "zLoggerAppenderConsole.h"
// TODO: Replace array with an hash table.
-static zArray<zLogger*>* g_loggers = new zArray<zLogger*>(YES, 32, NULL);
+static zArray<zLogger*>* g_loggers = new zArray<zLogger*>(YES, 32);
-zLogger::zLogger(char const* loggerName) : zObject(), _appenders(YES, 32, NULL) {
+zLogger::zLogger(char const* loggerName) : zObject(), _appenders(YES, 32) {
_id = zString(loggerName);
_level = LOG_LEVEL_INFO;
}
View
@@ -357,7 +357,7 @@ zString zString::from_pascal_string(unsigned char const* pascalString, int buffe
zArray<zString> zString::split(zString const& tokenizer, bool ignore_empty_token) const {
- zArray<zString> res(YES, 12, "");
+ zArray<zString> res(YES, 12);
int curPos = 0;
while (curPos < get_length()) {
View
@@ -5,7 +5,7 @@
#include <stdarg.h>
-zStringBuilder::zStringBuilder(void) : _strings(YES, 32, NULL) {
+zStringBuilder::zStringBuilder(void) : _strings(YES, 32) {
}
View
@@ -6,7 +6,7 @@
#include <stdio.h>
-zTester::zTester(void) : _tests(YES, 32, NULL) {
+zTester::zTester(void) : _tests(YES, 32) {
//
}
@@ -75,7 +75,7 @@ bool zTester::process_interactive(void) {
zStringBuilder commandBuf;
char c = 0;
- while ((c = getchar()) != '\n') {
+ while ((c = (char)getchar()) != '\n') {
commandBuf.append(c);
}
zString command = commandBuf.to_string().to_lowercase();
View
@@ -25,6 +25,7 @@
# include <unistd.h>
#endif
+#include "zGetOpt.h"
#include "zLogger.h"
#include "zFile.h"
#include "zStringBuilder.h"
@@ -72,15 +73,20 @@ void handleInvalidArg(char* programName, char invalidArg);
int main(int argc, char** argv) {
+ zGetOpt* getopt = new zGetOpt(argc, argv);
+ getopt->add_arg('h', "help", false, false, "Show this message.");
+ getopt->add_arg('v', "version", false, false, "Show version information and exit..");
+ getopt->add_arg('i', "interactive", false, false, "Enable the interactive mode.");
+
+ getopt->parse();
+
- int opt = 0;
+ zGetOpt::Argument* arg = NULL;
bool interactive = false;
bool use_dmalloc = false;
-#if defined(WIN32)
-#else
- while ((opt = getopt(argc, argv, "hvid")) != -1) {
- switch (opt) {
+ while ((arg = getopt->next()) != NULL) {
+ switch (arg->arg) {
case 'h':
showHelp(argv[0]);
exit(EXIT_SUCCESS);
@@ -96,12 +102,10 @@ int main(int argc, char** argv) {
use_dmalloc = true;
break;
default:
- handleInvalidArg(argv[0], opt);
+ handleInvalidArg(argv[0], arg->arg);
exit(EXIT_FAILURE);
}
}
-#endif
-
showCopyright(argv[0]);
#if defined(DMALLOC)
@@ -150,6 +154,7 @@ int main(int argc, char** argv) {
delete tester;
g_logger->shutdown();
g_logger->release_reference();
+ delete getopt;
#if defined(_CRTDBG_MAP_ALLOC)
if (_CrtDumpMemoryLeaks()) {
@@ -183,7 +188,7 @@ void showHelp(char* programName) {
printf(" -v show version information and exit.\n");
printf(" -i interactive mode.\n");
#if defined(DMALLOC)
- printf(" -d debug memory with dmalloc.\n");
+ printf(" -d debug memory (with dmalloc on Linux or Ms CRT).\n");
#endif
printf("\n");
printf("Report %s bugs to %s.\n", programName, PACKAGE_BUGREPORT);
View
@@ -5,6 +5,7 @@
#include "zThread.h"
#include "zRunnable.h"
#include "zReference.h"
+#include "zString.h"
zArrayTest::zArrayTest(void) {
}
@@ -64,13 +65,13 @@ char const* zArrayTest::get_test_description(int index) {
bool zArrayTest::test_ctor(void) {
{
- zArray<int>* array = new zArray<int>(NO, 1024, -1);
+ zArray<int>* array = new zArray<int>(NO, 1024);
array->append(0);
if (array->get_count() != 1) return false;
delete array;
}
{
- zArray<int>* array = new zArray<int>(YES, 0, -1);
+ zArray<int>* array = new zArray<int>(YES, 0);
array->append(0);
if (array->get_count() != 1) return false;
delete array;
@@ -80,7 +81,7 @@ bool zArrayTest::test_ctor(void) {
bool zArrayTest::test_resize(void) {
- zArray<int> arr(YES, 5, -1);
+ zArray<int> arr(YES, 5);
int size = 16 * 1024;
for (int i = 0; i < size; i++) {
arr.append(i);
@@ -108,7 +109,13 @@ bool zArrayTest::test_resize(void) {
bool zArrayTest::test_memory(void) {
- zArray<void*>* array = new zArray<void*>(NO, 1, NULL);
+ zArray<zString>* array = new zArray<zString>(NO, 1);
+
+ char buf[16 * 1024];
+ memset(&buf, 'a', sizeof(buf));
+ array->append(zString(buf, sizeof(buf) -1));
+ array->append(zString(buf, sizeof(buf) -1));
+ array->append(zString(buf, sizeof(buf) -1));
delete array;
@@ -138,7 +145,7 @@ bool zArrayTest::test_multithread(void) {
zThread* thread1 = new zThread(impl);
zThread* thread2 = new zThread(impl);
- zArray<zref_t>* arr = new zArray<zref_t>(YES, 10, 0);
+ zArray<zref_t>* arr = new zArray<zref_t>(YES, 10);
zReference* ref = new zReference();
impl->array = arr;
@@ -172,7 +179,7 @@ bool zArrayTest::test_multithread(void) {
bool zArrayTest::test_get(void) {
- zArray<char> array(NO, 1, 0);
+ zArray<char> array(NO, 1);
// Test on failing get.
char tmp = 'a';
if (array.get(0, &tmp) || tmp != 'a') return false;
View
@@ -75,7 +75,7 @@ class ThreadTest : public zRunnable {
bool zThreadTest::test_join(void) {
// Create threads.
ThreadTest impl;
- zArray<zThread*> array(NO, 5, NULL);
+ zArray<zThread*> array(NO, 5);
int sleep = 500;
for (int i = 0; i < 50; i++) {
zThread* thread = new zThread(&impl);
@@ -104,7 +104,7 @@ bool zThreadTest::test_join(void) {
bool zThreadTest::test_memory(void) {
ThreadTest impl;
- zArray<zThread*> array(YES, 10, NULL);
+ zArray<zThread*> array(YES, 10);
int sleep = 100;
for (int i = 0; i < 500; i++) {
zThread* thread = new zThread(&impl);

0 comments on commit 8dd600f

Please sign in to comment.