Commit 0977815a authored by Jisi Liu's avatar Jisi Liu

fix "memory leaks" in protostream-object files.

Change-Id: I0aca56802d974cb03cb89c1a294f37068b5b9758
parent 01e060f2
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
#include <google/protobuf/util/internal/utility.h> #include <google/protobuf/util/internal/utility.h>
#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/map_util.h> #include <google/protobuf/stubs/map_util.h>
#include <google/protobuf/stubs/once.h>
#include <google/protobuf/stubs/status_macros.h> #include <google/protobuf/stubs/status_macros.h>
...@@ -648,46 +649,53 @@ Status ProtoStreamObjectSource::RenderFieldMask( ...@@ -648,46 +649,53 @@ Status ProtoStreamObjectSource::RenderFieldMask(
} }
hash_map<string, ProtoStreamObjectSource::TypeRenderer>* hash_map<string, ProtoStreamObjectSource::TypeRenderer>*
ProtoStreamObjectSource::CreateRendererMap() { ProtoStreamObjectSource::renderers_ = NULL;
hash_map<string, ProtoStreamObjectSource::TypeRenderer>* result = GOOGLE_PROTOBUF_DECLARE_ONCE(source_renderers_init_);
new hash_map<string, ProtoStreamObjectSource::TypeRenderer>();
(*result)["google.protobuf.Timestamp"] = void ProtoStreamObjectSource::InitRendererMap() {
renderers_ = new hash_map<string, ProtoStreamObjectSource::TypeRenderer>();
(*renderers_)["google.protobuf.Timestamp"] =
&ProtoStreamObjectSource::RenderTimestamp; &ProtoStreamObjectSource::RenderTimestamp;
(*result)["google.protobuf.Duration"] = (*renderers_)["google.protobuf.Duration"] =
&ProtoStreamObjectSource::RenderDuration; &ProtoStreamObjectSource::RenderDuration;
(*result)["google.protobuf.DoubleValue"] = (*renderers_)["google.protobuf.DoubleValue"] =
&ProtoStreamObjectSource::RenderDouble; &ProtoStreamObjectSource::RenderDouble;
(*result)["google.protobuf.FloatValue"] = (*renderers_)["google.protobuf.FloatValue"] =
&ProtoStreamObjectSource::RenderFloat; &ProtoStreamObjectSource::RenderFloat;
(*result)["google.protobuf.Int64Value"] = (*renderers_)["google.protobuf.Int64Value"] =
&ProtoStreamObjectSource::RenderInt64; &ProtoStreamObjectSource::RenderInt64;
(*result)["google.protobuf.UInt64Value"] = (*renderers_)["google.protobuf.UInt64Value"] =
&ProtoStreamObjectSource::RenderUInt64; &ProtoStreamObjectSource::RenderUInt64;
(*result)["google.protobuf.Int32Value"] = (*renderers_)["google.protobuf.Int32Value"] =
&ProtoStreamObjectSource::RenderInt32; &ProtoStreamObjectSource::RenderInt32;
(*result)["google.protobuf.UInt32Value"] = (*renderers_)["google.protobuf.UInt32Value"] =
&ProtoStreamObjectSource::RenderUInt32; &ProtoStreamObjectSource::RenderUInt32;
(*result)["google.protobuf.BoolValue"] = &ProtoStreamObjectSource::RenderBool; (*renderers_)["google.protobuf.BoolValue"] = &ProtoStreamObjectSource::RenderBool;
(*result)["google.protobuf.StringValue"] = (*renderers_)["google.protobuf.StringValue"] =
&ProtoStreamObjectSource::RenderString; &ProtoStreamObjectSource::RenderString;
(*result)["google.protobuf.BytesValue"] = (*renderers_)["google.protobuf.BytesValue"] =
&ProtoStreamObjectSource::RenderBytes; &ProtoStreamObjectSource::RenderBytes;
(*result)["google.protobuf.Any"] = &ProtoStreamObjectSource::RenderAny; (*renderers_)["google.protobuf.Any"] = &ProtoStreamObjectSource::RenderAny;
(*result)["google.protobuf.Struct"] = &ProtoStreamObjectSource::RenderStruct; (*renderers_)["google.protobuf.Struct"] = &ProtoStreamObjectSource::RenderStruct;
(*result)["google.protobuf.Value"] = (*renderers_)["google.protobuf.Value"] =
&ProtoStreamObjectSource::RenderStructValue; &ProtoStreamObjectSource::RenderStructValue;
(*result)["google.protobuf.ListValue"] = (*renderers_)["google.protobuf.ListValue"] =
&ProtoStreamObjectSource::RenderStructListValue; &ProtoStreamObjectSource::RenderStructListValue;
(*result)["google.protobuf.FieldMask"] = (*renderers_)["google.protobuf.FieldMask"] =
&ProtoStreamObjectSource::RenderFieldMask; &ProtoStreamObjectSource::RenderFieldMask;
return result; ::google::protobuf::internal::OnShutdown(&DeleteRendererMap);
}
void ProtoStreamObjectSource::DeleteRendererMap() {
delete ProtoStreamObjectSource::renderers_;
renderers_ = NULL;
} }
// static // static
ProtoStreamObjectSource::TypeRenderer* ProtoStreamObjectSource::TypeRenderer*
ProtoStreamObjectSource::FindTypeRenderer(const string& type_url) { ProtoStreamObjectSource::FindTypeRenderer(const string& type_url) {
static hash_map<string, TypeRenderer>* renderers = CreateRendererMap(); ::google::protobuf::GoogleOnceInit(&source_renderers_init_, &InitRendererMap);
return FindOrNull(*renderers, type_url); return FindOrNull(*renderers_, type_url);
} }
Status ProtoStreamObjectSource::RenderField( Status ProtoStreamObjectSource::RenderField(
......
...@@ -202,7 +202,9 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource { ...@@ -202,7 +202,9 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource {
const google::protobuf::Type& type, const google::protobuf::Type& type,
StringPiece name, ObjectWriter* ow); StringPiece name, ObjectWriter* ow);
static hash_map<string, TypeRenderer>* CreateRendererMap(); static hash_map<string, TypeRenderer>* renderers_;
static void InitRendererMap();
static void DeleteRendererMap();
static TypeRenderer* FindTypeRenderer(const string& type_url); static TypeRenderer* FindTypeRenderer(const string& type_url);
// Renders a field value to the ObjectWriter. // Renders a field value to the ObjectWriter.
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <google/protobuf/util/internal/object_location_tracker.h> #include <google/protobuf/util/internal/object_location_tracker.h>
#include <google/protobuf/util/internal/constants.h> #include <google/protobuf/util/internal/constants.h>
#include <google/protobuf/util/internal/utility.h> #include <google/protobuf/util/internal/utility.h>
#include <google/protobuf/stubs/once.h>
#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/map_util.h> #include <google/protobuf/stubs/map_util.h>
#include <google/protobuf/stubs/statusor.h> #include <google/protobuf/stubs/statusor.h>
...@@ -1365,60 +1366,67 @@ void ProtoStreamObjectWriter::RenderSimpleDataPiece( ...@@ -1365,60 +1366,67 @@ void ProtoStreamObjectWriter::RenderSimpleDataPiece(
// Map of functions that are responsible for rendering well known type // Map of functions that are responsible for rendering well known type
// represented by the key. // represented by the key.
hash_map<string, ProtoStreamObjectWriter::TypeRenderer>* hash_map<string, ProtoStreamObjectWriter::TypeRenderer>*
ProtoStreamObjectWriter::CreateRendererMap() { ProtoStreamObjectWriter::renderers_ = NULL;
google::protobuf::scoped_ptr<hash_map<string, ProtoStreamObjectWriter::TypeRenderer> > GOOGLE_PROTOBUF_DECLARE_ONCE(writer_renderers_init_);
result(new hash_map<string, ProtoStreamObjectWriter::TypeRenderer>());
(*result)["type.googleapis.com/google.protobuf.Timestamp"] = void ProtoStreamObjectWriter::InitRendererMap() {
renderers_ = new hash_map<string, ProtoStreamObjectWriter::TypeRenderer>();
(*renderers_)["type.googleapis.com/google.protobuf.Timestamp"] =
&ProtoStreamObjectWriter::RenderTimestamp; &ProtoStreamObjectWriter::RenderTimestamp;
(*result)["type.googleapis.com/google.protobuf.Duration"] = (*renderers_)["type.googleapis.com/google.protobuf.Duration"] =
&ProtoStreamObjectWriter::RenderDuration; &ProtoStreamObjectWriter::RenderDuration;
(*result)["type.googleapis.com/google.protobuf.FieldMask"] = (*renderers_)["type.googleapis.com/google.protobuf.FieldMask"] =
&ProtoStreamObjectWriter::RenderFieldMask; &ProtoStreamObjectWriter::RenderFieldMask;
(*result)["type.googleapis.com/google.protobuf.Double"] = (*renderers_)["type.googleapis.com/google.protobuf.Double"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.Float"] = (*renderers_)["type.googleapis.com/google.protobuf.Float"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.Int64"] = (*renderers_)["type.googleapis.com/google.protobuf.Int64"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.UInt64"] = (*renderers_)["type.googleapis.com/google.protobuf.UInt64"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.Int32"] = (*renderers_)["type.googleapis.com/google.protobuf.Int32"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.UInt32"] = (*renderers_)["type.googleapis.com/google.protobuf.UInt32"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.Bool"] = (*renderers_)["type.googleapis.com/google.protobuf.Bool"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.String"] = (*renderers_)["type.googleapis.com/google.protobuf.String"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.Bytes"] = (*renderers_)["type.googleapis.com/google.protobuf.Bytes"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.DoubleValue"] = (*renderers_)["type.googleapis.com/google.protobuf.DoubleValue"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.FloatValue"] = (*renderers_)["type.googleapis.com/google.protobuf.FloatValue"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.Int64Value"] = (*renderers_)["type.googleapis.com/google.protobuf.Int64Value"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.UInt64Value"] = (*renderers_)["type.googleapis.com/google.protobuf.UInt64Value"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.Int32Value"] = (*renderers_)["type.googleapis.com/google.protobuf.Int32Value"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.UInt32Value"] = (*renderers_)["type.googleapis.com/google.protobuf.UInt32Value"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.BoolValue"] = (*renderers_)["type.googleapis.com/google.protobuf.BoolValue"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.StringValue"] = (*renderers_)["type.googleapis.com/google.protobuf.StringValue"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.BytesValue"] = (*renderers_)["type.googleapis.com/google.protobuf.BytesValue"] =
&ProtoStreamObjectWriter::RenderWrapperType; &ProtoStreamObjectWriter::RenderWrapperType;
(*result)["type.googleapis.com/google.protobuf.Value"] = (*renderers_)["type.googleapis.com/google.protobuf.Value"] =
&ProtoStreamObjectWriter::RenderStructValue; &ProtoStreamObjectWriter::RenderStructValue;
return result.release(); ::google::protobuf::internal::OnShutdown(&DeleteRendererMap);
}
void ProtoStreamObjectWriter::DeleteRendererMap() {
delete ProtoStreamObjectWriter::renderers_;
renderers_ = NULL;
} }
ProtoStreamObjectWriter::TypeRenderer* ProtoStreamObjectWriter::TypeRenderer*
ProtoStreamObjectWriter::FindTypeRenderer(const string& type_url) { ProtoStreamObjectWriter::FindTypeRenderer(const string& type_url) {
static hash_map<string, TypeRenderer>* renderers = CreateRendererMap(); ::google::protobuf::GoogleOnceInit(&writer_renderers_init_, &InitRendererMap);
return FindOrNull(*renderers, type_url); return FindOrNull(*renderers_, type_url);
} }
ProtoStreamObjectWriter::ProtoElement::ElementType ProtoStreamObjectWriter::ProtoElement::ElementType
......
...@@ -407,7 +407,9 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectWriter : public StructuredObjectWriter ...@@ -407,7 +407,9 @@ class LIBPROTOBUF_EXPORT ProtoStreamObjectWriter : public StructuredObjectWriter
// Helper functions to create the map and find functions responsible for // Helper functions to create the map and find functions responsible for
// rendering well known types, keyed by type URL. // rendering well known types, keyed by type URL.
static hash_map<string, TypeRenderer>* CreateRendererMap(); static hash_map<string, TypeRenderer>* renderers_;
static void InitRendererMap();
static void DeleteRendererMap();
static TypeRenderer* FindTypeRenderer(const string& type_url); static TypeRenderer* FindTypeRenderer(const string& type_url);
// Returns the ProtoElement::ElementType for the given Type. // Returns the ProtoElement::ElementType for the given Type.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment