microsoft / cpp_client_telemetry

1DS C++ SDK
Apache License 2.0
86 stars 47 forks source link

Memory Leak in initializing database #1134

Open aks3090 opened 1 year ago

aks3090 commented 1 year ago

Describe your environment. This is happening for one of the customer of MIP sdk which used 1DS sdk. MIP sdk just 1DS is cross-platform sdk. Customer environment is windows/linux here.

Steps to reproduce. Using any memory sanitizer will detect this.

What is the expected behavior? No memory leak as we have customer who also use this for services.

What is the actual behavior? What did you see instead?

Additional context. Discussed with lalit , needed to call sqlite3_finalize..() when logManager is shutdown.

lalitb commented 1 year ago

Memory dump as shared by @aks3090 -

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7f103e232c5f in __interceptor_malloc ../../../../libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x7f103c2fd570 in sqlite3MemMalloc (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x694570)
    #2 0x7f103c2fe035 in mallocWithAlarm (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x695035)
    #3 0x7f103c2fe0e1 in sqlite3Malloc (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x6950e1)
    #4 0x7f103c2fe815 in sqlite3DbMallocRaw (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x695815)
    #5 0x7f103c301b95 in strAccumFinishRealloc (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x698b95)
    #6 0x7f103c301c59 in sqlite3StrAccumFinish (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x698c59)
    #7 0x7f103c3020af in sqlite3_vmprintf (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x6990af)
    #8 0x7f103c302192 in sqlite3_mprintf (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x699192)
    #9 0x7f103c37cf6f in sqlite3Pragma (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x713f6f)
    #10 0x7f103c3b6a6c in yy_reduce (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x74da6c)
    #11 0x7f103c3b7ee0 in sqlite3Parser (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x74eee0)
    #12 0x7f103c3b91bd in sqlite3RunParser (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x7501bd)
    #13 0x7f103c382162 in sqlite3Prepare (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x719162)
    #14 0x7f103c3823bb in sqlite3LockAndPrepare (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x7193bb)
    #15 0x7f103c3825ce in sqlite3_prepare_v2 (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x7195ce)
    #16 0x7f103c099034 in Microsoft::Applications::Events::OfflineStorage_SQLite::initializeDatabase() (/opt/3p/binary/drm-mip2004/lib/libmip_core.so+0x430034)

SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s).
FAIL mip_test (exit status: 1)

Reason for Leak:

The allocation of this memory happens during LogManager initialization. SqliteDB wrapper will create prepared statement objects during initialization - https://github.com/microsoft/cpp_client_telemetry/blob/4f3adbd82b30e58ff24785e1bdabe9a9f6f9d603/lib/offline/OfflineStorage_SQLite.cpp#L791-L793 These objects should be cleaned (by explicitly calling sqlite3_finalize..()) when LogManager is shutdown, but this is missing in code. Since this is one time allocation during startup, in general it should be safe to have. The memory leak will get significant only if the LogManager is frequently reinitialized while the application is running.

anPalmei commented 1 year ago

Hi, is there any update on this issue?

nmanis commented 1 year ago

This is pending for review in a future release given this is a one-time operation allocation.