Skip to content

Commit 8b4d0da

Browse files
committed
CXX-1458 Don't bother closing bson subobjects in stack dtor
1 parent 7e7f6e0 commit 8b4d0da

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/bsoncxx/builder/core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class core::impl {
215215
}
216216
}
217217

218-
~frame() {
218+
void close() {
219219
if (is_array) {
220220
if (!bson_append_array_end(parent, &bson)) {
221221
throw bsoncxx::exception{error_code::k_cannot_end_appending_array};

src/bsoncxx/private/stack.hh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,31 @@
2323
namespace bsoncxx {
2424
BSONCXX_INLINE_NAMESPACE_BEGIN
2525

26+
// Note: This stack is only intended for use with the 'frame' type in
27+
// builder core.cpp.
2628
template <typename T, std::size_t size>
2729
class stack {
2830
public:
2931
stack() : _bucket_index(0), _bucket_size(size), _is_empty(true) {}
3032

3133
~stack() {
3234
while (!empty()) {
33-
pop_back();
35+
// If you are using a stack<core::impl::frame> inside a
36+
// builder::core object, then either one of two things is
37+
// true:
38+
//
39+
// 1) core::impl::is_viewable is true, so the stack is
40+
// empty, we aren't going to get here.
41+
//
42+
// OR
43+
//
44+
// 2) The stack isn't empty, so the builder wasn't in a
45+
// viewable state when we were destroying its internal
46+
// stack. We have a partially constructed but
47+
// un-observable BSON document. We don't need to call
48+
// close, which might fail. Just call _dec to properly
49+
// invoke the non-failing frame destructor.
50+
_dec();
3451
}
3552

3653
while (!_buckets.empty()) {
@@ -59,6 +76,7 @@ class stack {
5976
}
6077

6178
void pop_back() {
79+
_get_ptr()->close();
6280
_dec();
6381
}
6482

0 commit comments

Comments
 (0)