Skip to content

Commit eaa3146

Browse files
committed
More accurate refcounting and deinitialization
1 parent 95eda45 commit eaa3146

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

Source/FreeImage/FreeImage.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ DllMain(HANDLE hModule, uint32_t ul_reason_for_call, LPVOID lpReserved) {
5050
break;
5151

5252
case DLL_PROCESS_DETACH :
53-
FreeImage_DeInitialise();
53+
if (lpReserved == nullptr) {
54+
FreeImage_DeInitialise();
55+
}
5456
break;
5557

5658
case DLL_THREAD_ATTACH :

Source/FreeImage/Plugin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ PluginsRegistrySingleton& PluginsRegistrySingleton::Instance()
587587
bool PluginsRegistrySingleton::AddRef()
588588
{
589589
bool firstRef = false;
590-
if (mRefCounter++ == 0) {
590+
if ((mRefCounter < std::numeric_limits<uint32_t>::max()) && (mRefCounter++ == 0)) {
591591
mInstance.reset(new PluginsRegistry());
592592
firstRef = true;
593593
}
@@ -597,7 +597,7 @@ bool PluginsRegistrySingleton::AddRef()
597597
bool PluginsRegistrySingleton::DecRef()
598598
{
599599
bool lastRef = false;
600-
if (--mRefCounter == 0) {
600+
if ((mRefCounter > 0) && (--mRefCounter == 0)) {
601601
mInstance.reset();
602602
lastRef = true;
603603
}

TestAPI/MainTestSuite.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ int main(int argc, char *argv[]) {
135135
testHeaderOnly();
136136
#endif
137137

138-
139-
#if defined(FREEIMAGE_LIB) || !defined(WIN32)
140-
FreeImage_DeInitialise();
141-
#endif
142-
143138
// other tests
144139
testConvertToFloat();
145140
testConvertToColor();
@@ -148,6 +143,10 @@ int main(int argc, char *argv[]) {
148143
testTmoLinear();
149144
testHistogram();
150145

146+
#if defined(FREEIMAGE_LIB) || !defined(WIN32)
147+
FreeImage_DeInitialise();
148+
#endif
149+
151150
return 0;
152151
}
153152

0 commit comments

Comments
 (0)