Skip to content

Commit b27592d

Browse files
authored
Merge pull request #68 from foodtiny/task/fix-memory-leak
Task/fix memory leak
2 parents 4e836c6 + 44e2723 commit b27592d

67 files changed

Lines changed: 1302 additions & 889 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# git rm --cached `git ls-files -i --exclude-from=.gitignore`
22
.DS_Store
3+
bin
34
/.idea
45
/CMakeFiles
56
/cmake-build-debug
@@ -22,4 +23,4 @@
2223
/test.txt
2324
/logging
2425
/json
25-
/native.iml
26+
/native.iml

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ after_script:
2020
after_success:
2121
- ./misc/memory_check
2222
notifications:
23-
slack:
24-
secure: o9QdIW3+j/aNslgHg11smJ1lDraArvCx+WT0ySqp6bXW0OYrQgG/JX6AxMhtNieGq7DnSFVKJgZFLr9B8VyqjiKRK6pseNXF0XhcxtS2fMsx/1kHYYmkzaiQZgntG0ZqMCvHbvRHucfzLHAyMGX0W8/E2BHnFvNXE9zXNLMzNTIZbigIV3BUcPvM/hkxw9DJ1KLJtm94uoszqFn10Y2joWQqJ6HAlGJrirLzgLSuUjE9mvKDBk9udnOpHFE22FPaj8aICYAJRTKtSvM4t8JO1W78bnuPjZf/GzHnkKC/O2EpE8L1WVOevJxJw889mLmTjWMICNVmB7wF14UOQpDTwQeDyvuDUtxx5V9TU0AuhfPO6TpThcymnHh/6CFjtJHmtHCTMLfR2HXCu3F6dv4C/OJeKVtjcoxbKn+mvyHOmiuU8dxc+BdbEfJngQj7jskJVssGbNZJ/3dlWmkzcw99l4t4MSNPTICWXw4Di2UwL8knpDwYvjPIlotU1uJH9YpaVO7ub8bClX9p75ajLac8bEE42rL1722vthMqVlD+3bIxJVuXG/Ri5dhKUoBciEKcBNMzOEs4x3yKSFvKc7NF9Fwwl1OL+L47HpJ2NceCrSKwCO9qKBDS9Ck1CHzs8ikl6UMp+Gpd1h7veBV7atIhyaXubIVDdthzoD3+U1gFT/0=
23+
slack: foodtiny:hzkLBCLrqjtgi9ctqwdQ034S

CMakeLists.txt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ set(DYLD_LIBRARY_PATH ${DYLD_LIBRARY_PATH})
9898
file(GLOB_RECURSE HEADERS
9999
./*.h
100100
library.hpp
101-
)
101+
)
102102

103103
# Include all C/C++ and Unix Assembly files
104104
file(GLOB_RECURSE SOURCES_C
@@ -110,11 +110,12 @@ file(GLOB_RECURSE SOURCES_C
110110
server/*
111111
storage/*
112112
string/*
113+
system/*
113114
thread/*
114115
type/*
115116
vendor/*
116117
validator/*
117-
)
118+
)
118119

119120
# Include all C unit test cases
120121
file(GLOB_RECURSE TESTS_C
@@ -126,11 +127,12 @@ file(GLOB_RECURSE TESTS_C
126127
server/*_test.c
127128
storage/*_test.c
128129
string/*_test.c
130+
system/*_test.c
129131
thread/*_test.c
130132
type/*_test.c
131133
vendor/*_test.c
132134
validator/*_test.c
133-
)
135+
)
134136

135137
# Remove all test files within c files
136138
foreach (test_c_file ${TESTS_C})
@@ -140,12 +142,12 @@ endforeach ()
140142
# Only C++ unit test files
141143
file(GLOB_RECURSE SOURCES_CPP
142144
java/**/*.cpp
143-
)
145+
)
144146

145147
# Only C++ unit test files
146148
file(GLOB_RECURSE TESTS_CPP
147149
java/**/*Test.cpp
148-
)
150+
)
149151

150152
# Remove all test files within cpp files
151153
foreach (test_cpp_file ${TESTS_CPP})
@@ -175,7 +177,7 @@ add_custom_target(
175177
# Check memory leak
176178
add_custom_target(
177179
leak
178-
COMMAND valgrind --track-origins=yes --error-exitcode=2 --show-leak-kinds=all --leak-check=full ./native_test
180+
COMMAND valgrind --track-origins=yes --error-exitcode=2 --leak-check=full ./native_test
179181
)
180182

181183
# Check memory leak
@@ -186,8 +188,8 @@ add_custom_target(
186188

187189
# Check memory leak
188190
add_custom_target(
189-
leak-c
190-
COMMAND valgrind --track-origins=yes --error-exitcode=2 --show-leak-kinds=all --leak-check=full ./native_test_c
191+
leak-c
192+
COMMAND valgrind --track-origins=yes --error-exitcode=2 --leak-check=full ./native_test_c
191193
)
192194

193195
# Check memory leak
@@ -203,6 +205,13 @@ install(FILES ${HEADERS} DESTINATION include/native)
203205
install(DIRECTORY java DESTINATION include/native)
204206
install(FILES ${HEADERS} DESTINATION include/native)
205207

208+
# Add uninstall
209+
add_custom_target(
210+
uninstall
211+
COMMAND rm -rf /usr/local/include/native
212+
COMMAND rm -rf /usr/local/lib/libnative*
213+
)
214+
206215
# Link library for OSX and other platform
207216
# Darwin platform no need to link realtime library (-lrt)
208217
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
Beside standard library, we would like to have a greater customization with important goals:
99

10-
- Blazing fast performance and low-level access with GAS & C
10+
- Blazing fast performance with small footprint and low-level access with GAS & C
1111
- Powerful structured programming in C++ for scalability
1212
- Awesome syntactically enhancement with C++ customization
1313
- Java standard packages for productivity & maintainability
14+
- Reduce memory leak via automatic storage and object oriented instead of pointer
1415
- Support third-party services for business features
1516

1617
This project is also useful for new developers in practical programming.

common/segment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ inline char *segment_pointer_char(char *target_param, int from, int to) {
3838
return strdup("");
3939
}
4040
char *target = strdup(target_param);
41-
int length= to - from + 1;
41+
int length = to - from + 1;
4242
if (to >= length_target) {
4343
length = length_target - from + 1;
4444
}

common/sort_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
TEST (Common, QuickSort) {
3232
srand(time(NULL));
33-
int *array_int = malloc(50 * sizeof(int));
33+
int *array_int = calloc(50, sizeof(int));
3434
int index;
3535
for (index = 0; index < 50; ++index) {
3636
array_int[ index ] = rand();

compress/miniz.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1];
612612
#define MZ_FREE(x) x, ((void)0)
613613
#define MZ_REALLOC(p, x) NULL
614614
#else
615-
#define MZ_MALLOC(x) malloc(x)
615+
#define MZ_MALLOC(x) calloc(x, sizeof(char))
616616
#define MZ_FREE(x) free(x)
617617
#define MZ_REALLOC(p, x) realloc(p, x)
618618
#endif

crypto/base64.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ base64_encode(const unsigned char *src, size_t len) {
1818
unsigned char tmp[3];
1919

2020
// alloc
21-
enc = (char *) malloc(0);
21+
enc = (char *) calloc(0, sizeof(char));
2222
if (NULL == enc) {
2323
return NULL;
2424
}
@@ -105,7 +105,7 @@ base64_decode_ex(const char *src, size_t len, size_t *decsize) {
105105
unsigned char tmp[4];
106106

107107
// alloc
108-
dec = (unsigned char *) malloc(0);
108+
dec = (unsigned char *) calloc(0, sizeof(unsigned char));
109109
if (NULL == dec) {
110110
return NULL;
111111
}

example/HelloWorld/Main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <native/library.hpp>
2+
#include "Person.hpp"
3+
int main()
4+
{
5+
Person person;
6+
person.setFirstName("First name");
7+
return 0;
8+
}

example/HelloWorld/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all:
2+
mkdir -p bin
3+
g++ -c -std=c++11 -o bin/main.o Main.cpp
4+
gcc -o bin/main bin/main.o /usr/local/lib/libnative.so -lstdc++
5+
./bin/main

0 commit comments

Comments
 (0)