Skip to content

Commit 1fc3e50

Browse files
author
Grok Compression
committed
memory management: library now handles de-initialization
1 parent 8b4150e commit 1fc3e50

File tree

13 files changed

+19
-28
lines changed

13 files changed

+19
-28
lines changed

bindings/rust/examples/initialize_grok.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ fn str_to_slice(src: &str) -> Result<[i8; 4096], GrokError> {
3939
/// Clean up resources safely
4040
fn cleanup(grk_cparameters: *mut _grk_cparameters) {
4141
unsafe {
42-
grk_deinitialize();
4342
libc::free(grk_cparameters as *mut libc::c_void);
4443
}
4544
}

bindings/swig/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Now Grok api methods can be imported, for example
2121
from grok_core import (
2222
GRK_TILE_CACHE_ALL,
2323
grk_initialize,
24-
grk_deinitialize,
2524
grk_decompress_parameters,
2625
grk_decompress_init,
2726
grk_decompress_read_header,

examples/core/core_compress.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char** argv)
224224
// cleanup
225225
grk_object_unref(codec);
226226
grk_object_unref(&inputImage->obj);
227-
grk_deinitialize();
228227

229228
return rc;
230229
}

examples/core/core_decompress.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char** argv)
989989
// cleanup
990990
grk_object_unref(codec_diff);
991991
grk_object_unref(codec_full);
992-
grk_deinitialize();
993992

994993
return rc;
995994
}

examples/core/core_decompress.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from PIL import Image, ImageChops # Assumes Pillow is installed
1919
from grok_core import (
2020
grk_initialize,
21-
grk_deinitialize,
2221
grk_decompress_parameters,
2322
grk_image_comp,
2423
grk_object,
@@ -135,7 +134,6 @@ def decompress_image(
135134
return False
136135

137136
grk_object_unref(codec)
138-
grk_deinitialize()
139137
return True
140138

141139

examples/core/core_simple.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char** argv)
220220
if(encInputImage)
221221
grk_object_unref(&encInputImage->obj);
222222
// note: since decImage was allocated by library, it will be cleaned up by library
223-
grk_deinitialize();
224223

225224
return rc;
226225
}

src/lib/codec/apps/GrkCompress.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,6 @@ int GrkCompress::main(int argc, const char** argv, grk_image* in_image, grk_stre
870870
goto cleanup;
871871
}
872872
cleanup:
873-
grk_deinitialize();
874873

875874
return success;
876875
}

src/lib/codec/apps/GrkDecompress.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,6 @@ int GrkDecompress::main(int argc, const char* argv[])
15101510
}
15111511
cleanup:
15121512
destoryParams(&initParams.parameters);
1513-
grk_deinitialize();
15141513
return rc;
15151514
}
15161515

src/lib/codec/apps/GrkDump.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ int GrkDump::main(int argc, const char* argv[])
360360
grk_object_unref(codec);
361361
if(fout)
362362
fclose(fout);
363-
grk_deinitialize();
364363

365364
return rc;
366365
}

src/lib/core/grok.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ static grk_object* grkDecompressCreate(grk::IStream* stream)
108108
return &codec->obj;
109109
}
110110

111+
static inline void grk_deinitialize()
112+
{
113+
grk_plugin_cleanup();
114+
ExecSingleton::destroy();
115+
}
116+
111117
static inline bool areStringsEqual(const char* lhs, const char* rhs)
112118
{
113119
if(lhs == nullptr && rhs == nullptr)
@@ -157,6 +163,18 @@ void grk_initialize(const char* pluginPath, uint32_t numThreads, bool* plugin_in
157163
*plugin_initialized = initState_.pluginInitialized_;
158164
return;
159165
}
166+
167+
// Register cleanup once, before doing init work
168+
static bool atexit_registered = false;
169+
if(!atexit_registered)
170+
{
171+
if(std::atexit(grk_deinitialize) != 0)
172+
{
173+
// Handle registration failure (e.g., log error)
174+
}
175+
atexit_registered = true;
176+
}
177+
160178
// 1. set up executor
161179
ExecSingleton::create(numThreads);
162180

@@ -200,12 +218,6 @@ void grk_initialize(const char* pluginPath, uint32_t numThreads, bool* plugin_in
200218
}
201219
}
202220

203-
GRK_API void GRK_CALLCONV grk_deinitialize()
204-
{
205-
grk_plugin_cleanup();
206-
ExecSingleton::destroy();
207-
}
208-
209221
GRK_API grk_object* GRK_CALLCONV grk_object_ref(grk_object* obj)
210222
{
211223
if(!obj)

0 commit comments

Comments
 (0)