Skip to content

Commit 7cf4eac

Browse files
committed
Do not free all thread strings in string pool
1 parent 369394f commit 7cf4eac

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

include/scratchcpp/string_pool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct StringPtr;
1111

1212
extern "C"
1313
{
14-
LIBSCRATCHCPP_EXPORT StringPtr *string_pool_new();
14+
LIBSCRATCHCPP_EXPORT StringPtr *string_pool_new(bool internal = false);
1515
LIBSCRATCHCPP_EXPORT void string_pool_free(StringPtr *str);
1616
}
1717

src/scratch/string_pool.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ extern "C"
2424
{
2525
/*!
2626
* Use this instead of dynamically allocating StringPtr.
27+
* \param[in] internal Whether the string should be deallocated when the current thread is removed (it's only used within the thread).
2728
* \note The returned string is uninitialized. Use e. g. string_assign_cstring() to initialize it.
2829
*/
29-
StringPtr *string_pool_new()
30+
StringPtr *string_pool_new(bool internal)
3031
{
3132
if (freeStrings.empty()) {
3233
auto str = std::make_unique<StringPtr>();
3334
StringPtr *ptr = str.get();
3435
assert(strings.find(str) == strings.cend());
3536
strings.insert(std::move(str));
3637

37-
if (currentThread)
38+
if (internal && currentThread)
3839
threadStrings[currentThread].insert(ptr);
3940

4041
return ptr;
@@ -45,7 +46,7 @@ extern "C"
4546
StringPtr *ptr = last->second;
4647
freeStrings.erase(last);
4748

48-
if (currentThread)
49+
if (internal && currentThread)
4950
threadStrings[currentThread].insert(ptr);
5051

5152
return ptr;
@@ -54,10 +55,8 @@ extern "C"
5455
/*! Invalidates the given StringPtr so that it can be used for new strings later. */
5556
void string_pool_free(StringPtr *str)
5657
{
57-
if (currentThread) {
58-
assert(threadStrings[currentThread].find(str) != threadStrings[currentThread].cend());
58+
if (currentThread)
5959
threadStrings[currentThread].erase(str);
60-
}
6160

6261
freeStrings.insert(std::pair<size_t, StringPtr *>(str->allocatedSize, str));
6362
}

0 commit comments

Comments
 (0)