From 5cee340ad343319a182c4ad00ae3de05fc7eb635 Mon Sep 17 00:00:00 2001
From: Wouter van Oortmerssen <aardappel@gmail.com>
Date: Tue, 16 Jan 2018 09:39:35 -0800
Subject: [PATCH] Cleaned up FlatBufferBuilder clearing.

Change-Id: I81cf4d268670bdd11d1d56ca9f2de78c120df842
Tested: on Linux.
---
 include/flatbuffers/flatbuffers.h | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h
index c1ed1d19..9d2e2486 100644
--- a/include/flatbuffers/flatbuffers.h
+++ b/include/flatbuffers/flatbuffers.h
@@ -538,23 +538,23 @@ class vector_downward {
     if (buf_) {
       assert(allocator_);
       allocator_->deallocate(buf_, reserved_);
+      buf_ = nullptr;
     }
-    reserved_ = 0;
-    buf_ = nullptr;
-    cur_ = nullptr;
-    scratch_ = nullptr;
+    clear();
   }
 
   void clear() {
     if (buf_) {
       cur_ = buf_ + reserved_;
-      scratch_ = buf_;
     } else {
       reserved_ = 0;
-      buf_ = nullptr;
       cur_ = nullptr;
-      scratch_ = nullptr;
     }
+    clear_scratch();
+  }
+
+  void clear_scratch() {
+    scratch_ = buf_;
   }
 
   // Relinquish the pointer to the caller.
@@ -563,10 +563,8 @@ class vector_downward {
                       size());
     allocator_ = nullptr;
     own_allocator_ = false;
-    reserved_ = 0;
     buf_ = nullptr;
-    cur_ = nullptr;
-    scratch_ = nullptr;
+    clear();
     return fb;
   }
 
@@ -1534,6 +1532,7 @@ class FlatBufferBuilder {
 
   void Finish(uoffset_t root, const char *file_identifier, bool size_prefix) {
     NotNested();
+    buf_.clear_scratch();
     // This will cause the whole buffer to be aligned.
     PreAlign((size_prefix ? sizeof(uoffset_t) : 0) + sizeof(uoffset_t) +
                  (file_identifier ? kFileIdentifierLength : 0),
-- 
GitLab