[bug](fix)fix Geo memory leak (#19116)

This commit is contained in:
Liqf
2023-04-27 09:04:10 +08:00
committed by GitHub
parent b5140bc9b4
commit d12fe4a7d2
6 changed files with 39 additions and 29 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -22,6 +22,7 @@
#include <memory>
#include <string>
#include "common/factory_creator.h"
#include "geo/geo_common.h"
#include "geo/wkt_parse_type.h"
@ -70,6 +71,8 @@ protected:
};
class GeoPoint : public GeoShape {
ENABLE_FACTORY_CREATOR(GeoPoint);
public:
GeoPoint();
~GeoPoint() override;
@ -106,6 +109,8 @@ private:
};
class GeoLine : public GeoShape {
ENABLE_FACTORY_CREATOR(GeoLine);
public:
GeoLine();
~GeoLine() override;
@ -131,12 +136,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(); }
@ -157,6 +164,8 @@ private:
};
class GeoCircle : public GeoShape {
ENABLE_FACTORY_CREATOR(GeoCircle);
public:
GeoCircle();
~GeoCircle() override;

View File

@ -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;

View File

@ -20,6 +20,7 @@
#include <stdint.h>
#include <iosfwd>
#include <memory>
#include "geo/geo_common.h"
#include "geo/wkt_parse_type.h"
@ -41,14 +42,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);

View File

@ -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;