Skip to content

Commit 427add3

Browse files
Merge pull request #1724 from arcaneframework/dev/gg-do-not-throw-in-memory-pool-free
Do not throw an exception in 'MemoryPool::freeMemory()'
2 parents 80c14a8 + 459232c commit 427add3

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

arcane/src/arcane/utils/MemoryPool.cc

+31-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "arcane/utils/FatalErrorException.h"
1717
#include "arcane/utils/PlatformUtils.h"
18+
#include "arcane/utils/ValueConvert.h"
1819

1920
#include <unordered_map>
2021
#include <map>
@@ -53,13 +54,27 @@ class MemoryPool::Impl
5354
{
5455
std::unique_lock<std::mutex> lg(m_mutex);
5556
auto x = m_allocated_memory_map.find(ptr);
56-
if (x == m_allocated_memory_map.end())
57-
ARCANE_FATAL("MemoryPool '{0}': pointer {1} is not in the allocated map", m_name, ptr);
57+
if (x == m_allocated_memory_map.end()) {
58+
++m_nb_error;
59+
String str = String::format("MemoryPool '{0}': pointer {1} is not in the allocated map", m_name, ptr);
60+
if (m_is_throw_on_error)
61+
ARCANE_FATAL(str);
62+
else {
63+
std::cerr << "ERROR: " << str << "\n";
64+
return;
65+
}
66+
}
5867

5968
size_t allocated_size = x->second;
60-
if (size != allocated_size)
61-
ARCANE_FATAL("MemoryPool '{0}': Incoherent size saved_size={1} arg_size={2}",
62-
m_name, allocated_size, size);
69+
if (size != allocated_size) {
70+
++m_nb_error;
71+
String str = String::format("MemoryPool '{0}': Incoherent size saved_size={1} arg_size={2}",
72+
m_name, allocated_size, size);
73+
if (m_is_throw_on_error)
74+
ARCANE_FATAL(str);
75+
else
76+
std::cerr << "ERROR: " << str << "\n";
77+
}
6378

6479
m_allocated_memory_map.erase(x);
6580
}
@@ -81,10 +96,16 @@ class MemoryPool::Impl
8196
return m_allocated_memory_map.size();
8297
}
8398

99+
bool isThrowOnError() const { return m_is_throw_on_error; }
100+
void setIsThrowOnError(bool v) { m_is_throw_on_error = v; }
101+
Int32 nbError() const { return m_nb_error; }
102+
84103
private:
85104

86105
MapType m_allocated_memory_map;
87106
String m_name;
107+
std::atomic<Int32> m_nb_error = 0;
108+
bool m_is_throw_on_error = false;
88109
mutable std::mutex m_mutex;
89110
};
90111

@@ -168,6 +189,10 @@ class MemoryPool::Impl
168189
, m_free_map(name)
169190
, m_name(name)
170191
{
192+
if (auto v = Convert::Type<Int32>::tryParseFromEnvironment("ARCANE_MEMORY_POOL_THROW_ON_ERROR", true)) {
193+
bool throw_on_error = (v.value() != 0);
194+
m_allocated_map.setIsThrowOnError(throw_on_error);
195+
}
171196
}
172197

173198
public:
@@ -271,6 +296,7 @@ dumpStats(std::ostream& ostr)
271296
<< " nb_allocated=" << m_allocated_map.size()
272297
<< " nb_free=" << m_free_map.size()
273298
<< " nb_cached=" << m_nb_cached
299+
<< " nb_error=" << m_allocated_map.nbError()
274300
<< "\n";
275301
}
276302

0 commit comments

Comments
 (0)