Navigation Menu

Skip to content

Commit

Permalink
修复析构函数调用时机
Browse files Browse the repository at this point in the history
  • Loading branch information
pqpo committed Jul 18, 2018
1 parent e4f4050 commit cad9ad0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
Binary file added jniLibs/armeabi-v7a/liblog4a-lib.so
Binary file not shown.
7 changes: 7 additions & 0 deletions librarylog4a/src/main/cpp/FlushBuffer.cpp
Expand Up @@ -9,6 +9,9 @@ FlushBuffer::~FlushBuffer() {
if (data_ptr != nullptr) {
delete[] data_ptr;
}
if (release != nullptr) {
delete release;
}
}

size_t FlushBuffer::length() {
Expand Down Expand Up @@ -62,4 +65,8 @@ FILE *FlushBuffer::logFile() {
return log_file;
}

void FlushBuffer::releaseThis(void *release) {
this->release = release;
}


10 changes: 10 additions & 0 deletions librarylog4a/src/main/cpp/LogBuffer.cpp
Expand Up @@ -74,7 +74,14 @@ void LogBuffer::async_flush() {
}

void LogBuffer::async_flush(AsyncFileFlush *fileFlush) {
async_flush(fileFlush, nullptr);
}

void LogBuffer::async_flush(AsyncFileFlush *fileFlush, void *releaseThis) {
if(fileFlush == nullptr) {
if (releaseThis != nullptr) {
delete releaseThis;
}
return;
}
std::lock_guard<std::recursive_mutex> lck_clear(log_mtx);
Expand All @@ -84,8 +91,11 @@ void LogBuffer::async_flush(AsyncFileFlush *fileFlush) {
}
FlushBuffer* flushBuffer = new FlushBuffer(log_file);
flushBuffer->write(data_ptr, length());
flushBuffer->releaseThis(releaseThis);
clear();
fileFlush->async_flush(flushBuffer);
} else if (releaseThis != nullptr) {
delete releaseThis;
}
}

Expand Down
6 changes: 4 additions & 2 deletions librarylog4a/src/main/cpp/includes/FlushBuffer.h
Expand Up @@ -21,9 +21,11 @@ class FlushBuffer {
void* ptr();
FILE* logFile();

private:
FILE* log_file = nullptr;
void releaseThis(void *release);

private:
FILE* log_file = nullptr;
void* release = nullptr;
char* data_ptr = nullptr;
char* write_ptr = nullptr;
size_t capacity;
Expand Down
1 change: 1 addition & 0 deletions librarylog4a/src/main/cpp/includes/LogBuffer.h
Expand Up @@ -34,6 +34,7 @@ class LogBuffer {
void setAsyncFileFlush(AsyncFileFlush *fileFlush);
void async_flush();
void async_flush(AsyncFileFlush *fileFlush);
void async_flush(AsyncFileFlush *fileFlush, void *releaseThis);
void changeLogPath(char *log_path);

public:
Expand Down
11 changes: 6 additions & 5 deletions librarylog4a/src/main/cpp/log4a-lib.cpp
Expand Up @@ -67,10 +67,12 @@ static void writeDirtyLogToFile(int buffer_fd) {
if(buffered_size > 0) {
char *buffer_ptr_tmp = (char *) mmap(0, buffered_size, PROT_WRITE | PROT_READ, MAP_SHARED, buffer_fd, 0);
if (buffer_ptr_tmp != MAP_FAILED) {
LogBuffer tmp(buffer_ptr_tmp, buffered_size);
size_t data_size = tmp.length();
LogBuffer *tmp = new LogBuffer(buffer_ptr_tmp, buffered_size);
size_t data_size = tmp -> length();
if (data_size > 0) {
tmp.async_flush(fileFlush);
tmp -> async_flush(fileFlush, tmp);
} else {
delete tmp;
}
}
}
Expand All @@ -92,8 +94,7 @@ static void writeNative(JNIEnv *env, jobject instance, jlong ptr,

static void releaseNative(JNIEnv *env, jobject instance, jlong ptr) {
LogBuffer* logBuffer = reinterpret_cast<LogBuffer*>(ptr);
logBuffer->async_flush(fileFlush);
delete logBuffer;
logBuffer->async_flush(fileFlush, logBuffer);
if (fileFlush != nullptr) {
delete fileFlush;
}
Expand Down

0 comments on commit cad9ad0

Please sign in to comment.