puntoexe / Imebra (http://imebra.com/)

Imebra is a multiplatform open source C++ Dicom library from Puntoexe.

Clone this repository (size: 1.0 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/puntoexe/imebra/
commit 133: 6523eae8de23
parent 132: 7578d47a7e81
branch: default
- Fixes #56 (The external lock in baseObject may create circular references)
pa...@puntoexe.com
8 months ago

Changed (Δ1.2 KB):

raw changeset »

project_files/base/src/baseObject.cpp (23 lines added, 62 lines removed)

project_files/base/src/exception.cpp (2 lines added, 10 lines removed)

Up to file-list project_files/base/src/baseObject.cpp:

@@ -132,8 +132,9 @@ void basePtr::addRef()
132
132
//
133
133
///////////////////////////////////////////////////////////
134
134
///////////////////////////////////////////////////////////
135
baseObject::baseObject():m_lockCounter(0), m_bValid(true), m_pExceptionsManager(exceptionsManager::getExceptionsManager())
135
baseObject::baseObject(): m_pCriticalSection(new CObjectCriticalSection), m_lockCounter(0), m_bValid(true)
136
136
{
137
    m_pCriticalSection->addRef();
137
138
}
138
139
139
140
@@ -146,47 +147,18 @@ baseObject::baseObject():m_lockCounter(0
146
147
//
147
148
///////////////////////////////////////////////////////////
148
149
///////////////////////////////////////////////////////////
149
baseObject::baseObject(const ptr<baseObject>& externalLock): m_externalLock(externalLock),
150
	m_lockCounter(0), m_bValid(true), m_pExceptionsManager(exceptionsManager::getExceptionsManager())
150
baseObject::baseObject(const ptr<baseObject>& externalLock): 
151
	m_lockCounter(0), m_bValid(true)
151
152
{
152
}
153
154
155
///////////////////////////////////////////////////////////
156
///////////////////////////////////////////////////////////
157
//
158
//
159
// Constructor with optional increase of the exceptions
160
//  manager's counter
161
//
162
//
163
///////////////////////////////////////////////////////////
164
///////////////////////////////////////////////////////////
165
baseObject::baseObject(bool bIncreaseExceptionsManager) : m_lockCounter(0), m_bValid(true)
166
{
167
	if(bIncreaseExceptionsManager)
168
	{
169
		m_pExceptionsManager = exceptionsManager::getExceptionsManager();
170
	}
171
}
172
173
174
///////////////////////////////////////////////////////////
175
///////////////////////////////////////////////////////////
176
//
177
//
178
// Return the object used to lock this one
179
//
180
//
181
///////////////////////////////////////////////////////////
182
///////////////////////////////////////////////////////////
183
baseObject* baseObject::getExternalLock()
184
{
185
	if(m_externalLock == 0)
186
	{
187
		return this;
188
	}
189
	return m_externalLock->getExternalLock();
153
    if(externalLock == 0)
154
    {
155
        m_pCriticalSection = new CObjectCriticalSection;
156
    }
157
    else
158
    {
159
        m_pCriticalSection = externalLock->m_pCriticalSection;
160
    }
161
    m_pCriticalSection->addRef();
190
162
}
191
163
192
164
@@ -201,7 +173,7 @@ baseObject* baseObject::getExternalLock(
201
173
///////////////////////////////////////////////////////////
202
174
bool baseObject::isReferencedOnce()
203
175
{
204
	lockCriticalSection lockThis(&m_criticalSection);
176
	lockCriticalSection lockThis(&m_counterCriticalSection);
205
177
	return m_lockCounter == 1;
206
178
}
207
179
@@ -217,7 +189,8 @@ bool baseObject::isReferencedOnce()
217
189
///////////////////////////////////////////////////////////
218
190
baseObject::~baseObject()
219
191
{
220
	m_bValid = false;
192
    m_pCriticalSection->release();
193
    m_bValid = false;
221
194
}
222
195
223
196
@@ -237,9 +210,8 @@ void baseObject::addRef()
237
210
		return;
238
211
	}
239
212
240
	m_criticalSection.lock();
213
        lockCriticalSection lockThis(&m_counterCriticalSection);
241
214
	++m_lockCounter;
242
	m_criticalSection.unlock();
243
215
}
244
216
245
217
@@ -266,7 +238,7 @@ void baseObject::release()
266
238
	// Decrease the reference counter
267
239
	///////////////////////////////////////////////////////////
268
240
	{
269
		lockCriticalSection lockThis(&m_criticalSection);
241
                lockCriticalSection lockThis(&m_counterCriticalSection);
270
242
		if(--m_lockCounter != 0)
271
243
		{
272
244
			return;
@@ -313,12 +285,7 @@ void baseObject::lock()
313
285
	{
314
286
		return;
315
287
	}
316
	if(m_externalLock != 0)
317
	{
318
		m_externalLock->lock();
319
		return;
320
	}
321
	m_criticalSection.lock();
288
	m_pCriticalSection->m_criticalSection.lock();
322
289
}
323
290
324
291
@@ -337,13 +304,7 @@ void baseObject::unlock()
337
304
	{
338
305
		return;
339
306
	}
340
	if(m_externalLock != 0)
341
	{
342
		m_externalLock->unlock();
343
		return;
344
	}
345
	
346
	m_criticalSection.unlock();
307
	m_pCriticalSection->m_criticalSection.unlock();
347
308
}
348
309
349
310
@@ -448,8 +409,7 @@ lockMultipleObjects::lockMultipleObjects
448
409
		{
449
410
			continue;
450
411
		}
451
		ptr<baseObject> lockObject = (*scanObjects)->getExternalLock();
452
		csList.push_back(&( (*scanObjects)->m_criticalSection) );
412
		csList.push_back(&( (*scanObjects)->m_pCriticalSection->m_criticalSection) );
453
413
	}
454
414
	m_pLockedCS.reset(puntoexe::lockMultipleCriticalSections(&csList));
455
415
}
@@ -485,7 +445,8 @@ void lockMultipleObjects::unlock()
485
445
	{
486
446
		return;
487
447
	}
488
	puntoexe::unlockMultipleCriticalSections(m_pLockedCS.release());
448
	puntoexe::unlockMultipleCriticalSections(m_pLockedCS.get());
449
        m_pLockedCS.reset();
489
450
}
490
451
491
452

Up to file-list project_files/base/src/exception.cpp:

@@ -21,12 +21,6 @@ static exceptionsManager::forceException
21
21
22
22
	
23
23
///////////////////////////////////////////////////////////
24
// Construct without increasing my reference counter
25
///////////////////////////////////////////////////////////
26
exceptionsManager::exceptionsManager(): baseObject(false)
27
{}
28
29
///////////////////////////////////////////////////////////
30
24
// Return the message info for the specified thread
31
25
///////////////////////////////////////////////////////////
32
26
std::wstring exceptionsManager::getMessage()
@@ -99,12 +93,10 @@ void exceptionsManager::clearExceptionIn
99
93
///////////////////////////////////////////////////////////
100
94
// Return a pointer to the exceptions manager
101
95
///////////////////////////////////////////////////////////
102
exceptionsManager* exceptionsManager::getExceptionsManager()
96
ptr<exceptionsManager> exceptionsManager::getExceptionsManager()
103
97
{
104
98
	static ptr<exceptionsManager> m_manager(new exceptionsManager);
105
	static exceptionsManager* const m_pManager(m_manager.get());
106
	
107
	return m_pManager;
99
	return m_manager;
108
100
}
109
101
110
102