Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Memory leak checker API. #3368

Merged
merged 4 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions runtime/src/main/cpp/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ FrameOverlay exportFrameOverlay;
volatile int allocCount = 0;
volatile int aliveMemoryStatesCount = 0;

KBoolean g_checkLeaks = KonanNeedDebugInfo;

// TODO: can we pass this variable as an explicit argument?
THREAD_LOCAL_VARIABLE MemoryState* memoryState = nullptr;
THREAD_LOCAL_VARIABLE FrameOverlay* currentFrame = nullptr;
Expand Down Expand Up @@ -1724,10 +1726,13 @@ void deinitMemory(MemoryState* memoryState) {
}
#else
#if USE_GC
if (IsStrictMemoryModel && lastMemoryState)
RuntimeAssert(allocCount == 0, "Memory leaks found");
#endif
#endif
if (IsStrictMemoryModel && lastMemoryState && allocCount > 0 && g_checkLeaks) {
char buf[1024];
konan::snprintf(buf, sizeof(buf), "Memory leaks detected, %d objects leaked\n", allocCount);
konan::consoleErrorUtf8(buf, konan::strnlen(buf, sizeof(buf)));
}
#endif // USE_GC
#endif // TRACE_MEMORY

PRINT_EVENT(memoryState)
DEINIT_EVENT(memoryState)
Expand Down Expand Up @@ -2941,4 +2946,13 @@ void Kotlin_Any_share(ObjHeader* obj) {
shareAny(obj);
}

KBoolean Konan_Platform_getMemoryLeakChecker() {
return g_checkLeaks;
}

void Konan_Platform_setMemoryLeakChecker(KBoolean value) {
g_checkLeaks = value;
}


} // extern "C"
1 change: 1 addition & 0 deletions runtime/src/main/cpp/Porting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ static void onThreadExitCallback(void* value) {
free(record);
record = next;
}
pthread_setspecific(terminationKey, nullptr);
}

static void onThreadExitInit() {
Expand Down
16 changes: 15 additions & 1 deletion runtime/src/main/kotlin/kotlin/native/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public object Platform {
public val isDebugBinary: Boolean
get() = Platform_isDebugBinary()

/**
* If the memory leak checker is activated, by default true in debug mode, false in release.
* When memory leak checker is activated, and leak detected on process termination process
* is terminated with non-zero exit code.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, forgot to push update doc.

Copy link
Collaborator

@SvyatoslavScherbina SvyatoslavScherbina Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message without termination is somewhat undiscoverable (including leaks in Kotlin/Native tests). Why not terminate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, OK, will update message as well.

*/
public var hasMemoryLeakChecker: Boolean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name doesn't correspond to the behaviour.
Maybe isMemoryLeakCheckerEnabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed.

get() = Platform_getMemoryLeakChecker()
set(value) = Platform_setMemoryLeakChecker(value)
}

@SymbolName("Konan_Platform_canAccessUnaligned")
Expand All @@ -99,4 +107,10 @@ private external fun Platform_getCpuArchitecture(): Int
private external fun Platform_getMemoryModel(): Int

@SymbolName("Konan_Platform_isDebugBinary")
private external fun Platform_isDebugBinary(): Boolean
private external fun Platform_isDebugBinary(): Boolean

@SymbolName("Konan_Platform_getMemoryLeakChecker")
private external fun Platform_getMemoryLeakChecker(): Boolean

@SymbolName("Konan_Platform_setMemoryLeakChecker")
private external fun Platform_setMemoryLeakChecker(value: Boolean): Unit