Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit c2221b8

Browse files
committed
Fix memory leaks in static variable pointers.
1 parent 071129f commit c2221b8

File tree

6 files changed

+16
-12
lines changed

6 files changed

+16
-12
lines changed

export/ClassFactory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#ifndef _INCLUDED_Field3D_ClassFactory_H_
4545
#define _INCLUDED_Field3D_ClassFactory_H_
4646

47+
#include <boost/scoped_ptr.hpp>
4748
#include <map>
4849
#include <vector>
4950

@@ -161,7 +162,7 @@ class FIELD3D_API ClassFactory
161162
NameVec m_fieldMappingIONames;
162163

163164
//! Pointer to static instance
164-
static ClassFactory *ms_instance;
165+
static boost::scoped_ptr<ClassFactory> ms_instance;
165166

166167
};
167168

export/FieldCache.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
//----------------------------------------------------------------------------//
4848

49+
#include <boost/scoped_ptr.hpp>
4950
#include <boost/thread/mutex.hpp>
5051
#include <boost/foreach.hpp>
5152

@@ -119,7 +120,7 @@ class FieldCache
119120
//! The cache itself. Maps a 'key' to a weak pointer and a raw pointer.
120121
Cache m_cache;
121122
//! The singleton instance
122-
static FieldCache *ms_singleton;
123+
static boost::scoped_ptr<FieldCache> ms_singleton;
123124
//! Mutex to prevent multiple allocaation of the singleton
124125
static boost::mutex ms_creationMutex;
125126
//! Mutex to prevent reading from and writing to the cache concurrently.
@@ -134,8 +135,8 @@ template <typename Data_T>
134135
FieldCache<Data_T>& FieldCache<Data_T>::singleton()
135136
{
136137
boost::mutex::scoped_lock lock(ms_creationMutex);
137-
if (!ms_singleton) {
138-
ms_singleton = new FieldCache;
138+
if (ms_singleton.get() == NULL) {
139+
ms_singleton.reset(new FieldCache);
139140
}
140141
return *ms_singleton;
141142
}

export/SparseFile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
//----------------------------------------------------------------------------//
4848

49+
#include <boost/scoped_ptr.hpp>
4950
#include <deque>
5051
#include <list>
5152
#include <vector>
@@ -494,7 +495,7 @@ class FIELD3D_API SparseFileManager
494495
SparseFileManager();
495496

496497
//! Pointer to singleton
497-
static SparseFileManager *ms_singleton;
498+
static boost::scoped_ptr<SparseFileManager> ms_singleton;
498499

499500
//! Adds the newly loaded block to the cache, managed by the paging algorithm
500501
void addBlockToCache(DataTypeEnum blockType, int fileId, int blockIdx);

src/ClassFactory.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ FIELD3D_NAMESPACE_OPEN
5656
// Static instances
5757
//----------------------------------------------------------------------------//
5858

59-
ClassFactory* ClassFactory::ms_instance = NULL;
59+
boost::scoped_ptr<ClassFactory> ClassFactory::ms_instance;
6060

6161
//----------------------------------------------------------------------------//
6262
// ClassFactory implementations
@@ -277,8 +277,9 @@ ClassFactory::createFieldMappingIO(const std::string &className) const
277277
ClassFactory&
278278
ClassFactory::singleton()
279279
{
280-
if (!ms_instance)
281-
ms_instance = new ClassFactory;
280+
if (ms_instance.get() == NULL) {
281+
ms_instance.reset(new ClassFactory);
282+
}
282283
return *ms_instance;
283284
}
284285

src/FieldCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ boost::mutex FieldCache<Data_T>::ms_creationMutex;
5858
template <typename Data_T>
5959
boost::mutex FieldCache<Data_T>::ms_accessMutex;
6060
template <typename Data_T>
61-
FieldCache<Data_T>* FieldCache<Data_T>::ms_singleton;
61+
boost::scoped_ptr<FieldCache<Data_T> > FieldCache<Data_T>::ms_singleton;
6262

6363
template class FieldCache<half>;
6464
template class FieldCache<float>;

src/SparseFile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ FIELD3D_NAMESPACE_OPEN
5757
// Static instances
5858
//----------------------------------------------------------------------------//
5959

60-
SparseFileManager *SparseFileManager::ms_singleton = 0;
60+
boost::scoped_ptr<SparseFileManager> SparseFileManager::ms_singleton;
6161

6262
//----------------------------------------------------------------------------//
6363
// SparseFileManager
6464
//----------------------------------------------------------------------------//
6565

6666
SparseFileManager & SparseFileManager::singleton()
6767
{
68-
if (!ms_singleton) {
69-
ms_singleton = new SparseFileManager;
68+
if (ms_singleton.get() == NULL) {
69+
ms_singleton.reset(new SparseFileManager);
7070
}
7171
return *ms_singleton;
7272
}

0 commit comments

Comments
 (0)