You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/04/27 07:42:55 UTC

[doris] 04/10: [bug](fix)fix Geo memory leak (#19116)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch branch-2.0-alpha
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 967ed16cf83294c40048ac44b66cb687a1ae5110
Author: Liqf <10...@users.noreply.github.com>
AuthorDate: Thu Apr 27 09:04:10 2023 +0800

    [bug](fix)fix Geo memory leak (#19116)
---
 be/src/geo/geo_tobinary.cpp |  2 +-
 be/src/geo/geo_types.cpp    | 16 ++++++++--------
 be/src/geo/geo_types.h      | 11 ++++++++++-
 be/src/geo/wkb_parse.cpp    | 24 ++++++++++++------------
 be/src/geo/wkb_parse.h      |  9 +++++----
 be/src/geo/wkt_yacc.y       |  6 +++---
 6 files changed, 39 insertions(+), 29 deletions(-)

diff --git a/be/src/geo/geo_tobinary.cpp b/be/src/geo/geo_tobinary.cpp
index 46ffc1b0f0..0e64c00168 100644
--- a/be/src/geo/geo_tobinary.cpp
+++ b/be/src/geo/geo_tobinary.cpp
@@ -88,7 +88,7 @@ bool toBinary::writeGeoPolygon(doris::GeoPolygon* polygon, ToBinaryContext* ctx)
     writeByteOrder(ctx);
     writeGeometryType(wkbType::wkbPolygon, ctx);
     writeInt(polygon->numLoops(), ctx);
-    GeoCoordinateListList* coordss = polygon->to_coords();
+    std::unique_ptr<GeoCoordinateListList> coordss(polygon->to_coords());
 
     for (int i = 0; i < coordss->list.size(); ++i) {
         writeCoordinateList(*coordss->list[i], true, ctx);
diff --git a/be/src/geo/geo_types.cpp b/be/src/geo/geo_types.cpp
index c134dbfb6e..ec06ffdfa2 100644
--- a/be/src/geo/geo_types.cpp
+++ b/be/src/geo/geo_types.cpp
@@ -219,19 +219,19 @@ GeoShape* GeoShape::from_encoded(const void* ptr, size_t size) {
     std::unique_ptr<GeoShape> shape;
     switch (((const char*)ptr)[1]) {
     case GEO_SHAPE_POINT: {
-        shape.reset(new GeoPoint());
+        shape.reset(GeoPoint::create_unique().release());
         break;
     }
     case GEO_SHAPE_LINE_STRING: {
-        shape.reset(new GeoLine());
+        shape.reset(GeoLine::create_unique().release());
         break;
     }
     case GEO_SHAPE_POLYGON: {
-        shape.reset(new GeoPolygon());
+        shape.reset(GeoPolygon::create_unique().release());
         break;
     }
     case GEO_SHAPE_CIRCLE: {
-        shape.reset(new GeoCircle());
+        shape.reset(GeoCircle::create_unique().release());
         break;
     }
     default:
@@ -274,10 +274,10 @@ GeoCoordinateList GeoLine::to_coords() const {
     return coords;
 }
 
-GeoCoordinateListList* GeoPolygon::to_coords() const {
-    GeoCoordinateListList* coordss = new GeoCoordinateListList();
+const std::unique_ptr<GeoCoordinateListList> GeoPolygon::to_coords() const {
+    std::unique_ptr<GeoCoordinateListList> coordss(new GeoCoordinateListList());
     for (int i = 0; i < GeoPolygon::numLoops(); ++i) {
-        GeoCoordinateList* coords = new GeoCoordinateList();
+        std::unique_ptr<GeoCoordinateList> coords(new GeoCoordinateList());
         S2Loop* loop = GeoPolygon::getLoop(i);
         for (int j = 0; j < loop->num_vertices(); ++j) {
             GeoCoordinate coord;
@@ -294,7 +294,7 @@ GeoCoordinateListList* GeoPolygon::to_coords() const {
                 coords->add(coord);
             }
         }
-        coordss->add(coords);
+        coordss->add(coords.release());
     }
     return coordss;
 }
diff --git a/be/src/geo/geo_types.h b/be/src/geo/geo_types.h
index 38022bb296..e75831d560 100644
--- a/be/src/geo/geo_types.h
+++ b/be/src/geo/geo_types.h
@@ -23,6 +23,7 @@
 #include <string>
 #include <vector>
 
+#include "common/factory_creator.h"
 #include "geo/geo_common.h"
 #include "geo/wkt_parse_type.h"
 
@@ -71,6 +72,8 @@ protected:
 };
 
 class GeoPoint : public GeoShape {
+    ENABLE_FACTORY_CREATOR(GeoPoint);
+
 public:
     GeoPoint();
     ~GeoPoint() override;
@@ -107,6 +110,8 @@ private:
 };
 
 class GeoLine : public GeoShape {
+    ENABLE_FACTORY_CREATOR(GeoLine);
+
 public:
     GeoLine();
     ~GeoLine() override;
@@ -132,12 +137,14 @@ private:
 };
 
 class GeoPolygon : public GeoShape {
+    ENABLE_FACTORY_CREATOR(GeoPolygon);
+
 public:
     GeoPolygon();
     ~GeoPolygon() override;
 
     GeoParseStatus from_coords(const GeoCoordinateListList& list);
-    GeoCoordinateListList* to_coords() const;
+    const std::unique_ptr<GeoCoordinateListList> to_coords() const;
 
     GeoShapeType type() const override { return GEO_SHAPE_POLYGON; }
     const S2Polygon* polygon() const { return _polygon.get(); }
@@ -158,6 +165,8 @@ private:
 };
 
 class GeoCircle : public GeoShape {
+    ENABLE_FACTORY_CREATOR(GeoCircle);
+
 public:
     GeoCircle();
     ~GeoCircle() override;
diff --git a/be/src/geo/wkb_parse.cpp b/be/src/geo/wkb_parse.cpp
index 2dafc1bbc7..e24328d756 100644
--- a/be/src/geo/wkb_parse.cpp
+++ b/be/src/geo/wkb_parse.cpp
@@ -127,7 +127,7 @@ WkbParseContext* WkbParse::read(std::istream& is, WkbParseContext* ctx) {
 
     ctx->dis = ByteOrderDataInStream(buf.data(), buf.size()); // will default to machine endian
 
-    ctx->shape = readGeometry(ctx);
+    ctx->shape = readGeometry(ctx).release();
 
     if (!ctx->shape) {
         ctx->parse_status = GEO_PARSE_WKB_SYNTAX_ERROR;
@@ -135,7 +135,7 @@ WkbParseContext* WkbParse::read(std::istream& is, WkbParseContext* ctx) {
     return ctx;
 }
 
-GeoShape* WkbParse::readGeometry(WkbParseContext* ctx) {
+std::unique_ptr<GeoShape> WkbParse::readGeometry(WkbParseContext* ctx) {
     // determine byte order
     unsigned char byteOrder = ctx->dis.readByte();
 
@@ -150,17 +150,17 @@ GeoShape* WkbParse::readGeometry(WkbParseContext* ctx) {
 
     uint32_t geometryType = (typeInt & 0xffff) % 1000;
 
-    GeoShape* shape;
+    std::unique_ptr<GeoShape> shape;
 
     switch (geometryType) {
     case wkbType::wkbPoint:
-        shape = readPoint(ctx);
+        shape.reset(readPoint(ctx).release());
         break;
     case wkbType::wkbLine:
-        shape = readLine(ctx);
+        shape.reset(readLine(ctx).release());
         break;
     case wkbType::wkbPolygon:
-        shape = readPolygon(ctx);
+        shape.reset(readPolygon(ctx).release());
         break;
     default:
         return nullptr;
@@ -168,9 +168,9 @@ GeoShape* WkbParse::readGeometry(WkbParseContext* ctx) {
     return shape;
 }
 
-GeoPoint* WkbParse::readPoint(WkbParseContext* ctx) {
+std::unique_ptr<GeoPoint> WkbParse::readPoint(WkbParseContext* ctx) {
     GeoCoordinateList coords = WkbParse::readCoordinateList(1, ctx);
-    GeoPoint* point = new GeoPoint();
+    std::unique_ptr<GeoPoint> point = GeoPoint::create_unique();
 
     if (point->from_coord(coords.list[0]) == GEO_PARSE_OK) {
         return point;
@@ -179,12 +179,12 @@ GeoPoint* WkbParse::readPoint(WkbParseContext* ctx) {
     }
 }
 
-GeoLine* WkbParse::readLine(WkbParseContext* ctx) {
+std::unique_ptr<GeoLine> WkbParse::readLine(WkbParseContext* ctx) {
     uint32_t size = ctx->dis.readUnsigned();
     minMemSize(wkbLine, size, ctx);
 
     GeoCoordinateList coords = WkbParse::readCoordinateList(size, ctx);
-    GeoLine* line = new GeoLine();
+    std::unique_ptr<GeoLine> line = GeoLine::create_unique();
 
     if (line->from_coords(coords) == GEO_PARSE_OK) {
         return line;
@@ -193,7 +193,7 @@ GeoLine* WkbParse::readLine(WkbParseContext* ctx) {
     }
 }
 
-GeoPolygon* WkbParse::readPolygon(WkbParseContext* ctx) {
+std::unique_ptr<GeoPolygon> WkbParse::readPolygon(WkbParseContext* ctx) {
     uint32_t num_loops = ctx->dis.readUnsigned();
     minMemSize(wkbPolygon, num_loops, ctx);
     GeoCoordinateListList coordss;
@@ -204,7 +204,7 @@ GeoPolygon* WkbParse::readPolygon(WkbParseContext* ctx) {
         coordss.add(coords);
     }
 
-    GeoPolygon* polygon = new GeoPolygon();
+    std::unique_ptr<GeoPolygon> polygon = GeoPolygon::create_unique();
 
     if (polygon->from_coords(coordss) == GEO_PARSE_OK) {
         return polygon;
diff --git a/be/src/geo/wkb_parse.h b/be/src/geo/wkb_parse.h
index 7ee3b1fe82..916e6a3ef8 100644
--- a/be/src/geo/wkb_parse.h
+++ b/be/src/geo/wkb_parse.h
@@ -20,6 +20,7 @@
 #include <stdint.h>
 
 #include <iosfwd>
+#include <memory>
 
 #include "geo/geo_common.h"
 #include "geo/geo_types.h"
@@ -43,14 +44,14 @@ public:
 
     static WkbParseContext* read(std::istream& is, WkbParseContext* ctx);
 
-    static GeoShape* readGeometry(WkbParseContext* ctx);
+    static std::unique_ptr<GeoShape> readGeometry(WkbParseContext* ctx);
 
 private:
-    static GeoPoint* readPoint(WkbParseContext* ctx);
+    static std::unique_ptr<GeoPoint> readPoint(WkbParseContext* ctx);
 
-    static GeoLine* readLine(WkbParseContext* ctx);
+    static std::unique_ptr<GeoLine> readLine(WkbParseContext* ctx);
 
-    static GeoPolygon* readPolygon(WkbParseContext* ctx);
+    static std::unique_ptr<GeoPolygon> readPolygon(WkbParseContext* ctx);
 
     static GeoCoordinateList readCoordinateList(unsigned size, WkbParseContext* ctx);
 
diff --git a/be/src/geo/wkt_yacc.y b/be/src/geo/wkt_yacc.y
index 7757cbef36..1af26e82ee 100644
--- a/be/src/geo/wkt_yacc.y
+++ b/be/src/geo/wkt_yacc.y
@@ -92,7 +92,7 @@ shape:
 point:
      KW_POINT '(' coordinate ')'
      {
-        std::unique_ptr<doris::GeoPoint> point(new doris::GeoPoint());
+        std::unique_ptr<doris::GeoPoint> point = doris::GeoPoint::create_unique();
         ctx->parse_status = point->from_coord($3);
         if (ctx->parse_status != doris::GEO_PARSE_OK) {
             YYABORT;
@@ -106,7 +106,7 @@ linestring:
     {
         // to avoid memory leak
         std::unique_ptr<doris::GeoCoordinateList> list($3);
-        std::unique_ptr<doris::GeoLine> line(new doris::GeoLine());
+        std::unique_ptr<doris::GeoLine> line = doris::GeoLine::create_unique();
         ctx->parse_status = line->from_coords(*$3);
         if (ctx->parse_status != doris::GEO_PARSE_OK) {
             YYABORT;
@@ -120,7 +120,7 @@ polygon:
     {
         // to avoid memory leak
         std::unique_ptr<doris::GeoCoordinateListList> list($3);
-        std::unique_ptr<doris::GeoPolygon> polygon(new doris::GeoPolygon());
+        std::unique_ptr<doris::GeoPolygon> polygon = doris::GeoPolygon::create_unique();
         ctx->parse_status = polygon->from_coords(*$3);
         if (ctx->parse_status != doris::GEO_PARSE_OK) {
             YYABORT;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org