Skip to content

Commit 8093468

Browse files
committed
MYCPP-271: Add missing virtual dtors and other memory leak fixes.
1 parent 4466702 commit 8093468

File tree

4 files changed

+62
-35
lines changed

4 files changed

+62
-35
lines changed

devapi/collection_crud.cc

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ class Op_collection_add
133133
}
134134

135135

136-
internal::BaseResult get_result() override
136+
internal::BaseResult mk_result(cdk::Reply *reply) override
137137
{
138-
return Result::Access::mk(m_sess, m_reply, m_id_list);
138+
return Result::Access::mk(m_sess, reply, m_id_list);
139139
}
140140

141141

@@ -339,15 +339,14 @@ class Op_collection_remove
339339

340340
cdk::Reply* send_command()
341341
{
342-
m_reply =
343-
new cdk::Reply(get_cdk_session().coll_remove(
344-
m_coll,
345-
get_where(),
346-
get_order_by(),
347-
get_limit(),
348-
get_params()
349-
));
350-
return m_reply;
342+
return
343+
new cdk::Reply(get_cdk_session().coll_remove(
344+
m_coll,
345+
get_where(),
346+
get_order_by(),
347+
get_limit(),
348+
get_params()
349+
));
351350
}
352351

353352
friend mysqlx::CollectionRemove;
@@ -405,18 +404,17 @@ class Op_collection_find
405404

406405
cdk::Reply* send_command()
407406
{
408-
m_reply =
409-
new cdk::Reply(get_cdk_session().coll_find(
410-
m_coll,
411-
get_where(),
412-
get_doc_proj(),
413-
get_order_by(),
414-
nullptr, // group_by
415-
nullptr, // having
416-
get_limit(),
417-
get_params()
418-
));
419-
return m_reply;
407+
return
408+
new cdk::Reply(get_cdk_session().coll_find(
409+
m_coll,
410+
get_where(),
411+
get_doc_proj(),
412+
get_order_by(),
413+
nullptr, // group_by
414+
nullptr, // having
415+
get_limit(),
416+
get_params()
417+
));
420418
}
421419

422420
friend mysqlx::CollectionFind;
@@ -509,7 +507,7 @@ class Op_collection_modify
509507
if (m_update.empty())
510508
return NULL;
511509

512-
m_reply =
510+
return
513511
new cdk::Reply(get_cdk_session().coll_update(
514512
m_coll,
515513
get_where(),
@@ -518,7 +516,6 @@ class Op_collection_modify
518516
get_limit(),
519517
get_params()
520518
));
521-
return m_reply;
522519
}
523520

524521
void add_operation(Field_Op::Operation op,

devapi/impl.h

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ struct internal::BaseResult::Access
518518
underlying CDK session. This produces a cdk::Reply object which
519519
is used for further processing. Sending the CRUD operation is
520520
performed by method `send_command` which should be overwriten by
521-
derived class. Derived class has access to underlying CDK session
521+
derived class. Derived class has access to the underlying CDK session
522522
with method `get_cdk_session()`.
523523
524524
2. After getting cdk::Reply object implementation waits for it to
@@ -541,7 +541,12 @@ class Op_base
541541
protected:
542542

543543
internal::XSession_base *m_sess;
544-
cdk::Reply *m_reply = NULL;
544+
545+
/*
546+
Note: using cdk::scoped_ptr to be able to trnasfer ownership
547+
to a different object.
548+
*/
549+
cdk::scoped_ptr<cdk::Reply> m_reply;
545550

546551
row_count_t m_limit = 0;
547552
bool m_has_limit = false;
@@ -562,6 +567,8 @@ class Op_base
562567
: m_sess(&tbl.getSession())
563568
{}
564569

570+
virtual ~Op_base()
571+
{}
565572

566573
cdk::Session& get_cdk_session()
567574
{
@@ -571,6 +578,21 @@ class Op_base
571578

572579
virtual cdk::Reply* send_command() = 0;
573580

581+
/*
582+
Given cdk reply object for the statement, return BaseResult object
583+
that handles that reply. The reply pointer can be NULL in case no
584+
reply has been generated for the statement (TODO: explain in what
585+
scenario reply can be NULL).
586+
587+
The returned BaseResult object should take ownership of the cdk reply
588+
object passed here (if any).
589+
*/
590+
591+
virtual internal::BaseResult mk_result(cdk::Reply *reply)
592+
{
593+
return reply ? internal::BaseResult::Access::mk(m_sess, reply)
594+
: internal::BaseResult::Access::mk_empty();
595+
}
574596

575597
// Limit and offset
576598

@@ -620,7 +642,7 @@ class Op_base
620642
if (m_inited)
621643
return;
622644
m_inited = true;
623-
m_reply = send_command();
645+
m_reply.reset(send_command());
624646
}
625647

626648
bool is_completed()
@@ -629,7 +651,7 @@ class Op_base
629651
return true;
630652

631653
init();
632-
m_completed = (NULL == m_reply) || m_reply->is_completed();
654+
m_completed = (!m_reply) || m_reply->is_completed();
633655
return m_completed;
634656
}
635657

@@ -654,17 +676,17 @@ class Op_base
654676
return get_result();
655677
}
656678

657-
virtual internal::BaseResult get_result()
679+
internal::BaseResult get_result()
658680
{
659681
if (!is_completed())
660682
THROW("Attempt to get result of incomplete operation");
661683

662-
// Note: BaseResult takes ownership of the cdk::Reply object.
684+
/*
685+
Note: result created by mk_result() takes ownership of the cdk::Reply
686+
object.
687+
*/
663688

664-
cdk::Reply *reply = m_reply;
665-
m_reply = NULL;
666-
return reply ? internal::BaseResult::Access::mk(m_sess, reply)
667-
: internal::BaseResult::Access::mk_empty();
689+
return mk_result(m_reply.release());
668690
}
669691

670692

devapi/result.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,10 @@ internal::BaseResult::~BaseResult()
12731273
try {
12741274
if (m_sess)
12751275
m_sess->deregister_result(this);
1276+
}
1277+
catch (...) {}
1278+
1279+
try {
12761280
if (m_owns_impl)
12771281
delete m_impl;
12781282
}
@@ -1282,6 +1286,9 @@ internal::BaseResult::~BaseResult()
12821286

12831287
void mysqlx::internal::BaseResult::init(mysqlx::internal::BaseResult &&init_)
12841288
{
1289+
if (m_impl && m_owns_impl)
1290+
delete m_impl;
1291+
12851292
m_pos = 0;
12861293
m_impl = init_.m_impl;
12871294
if (!init_.m_owns_impl)

include/devapi/statement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class XSession_base;
5555
struct Executable_impl
5656
{
5757
virtual BaseResult execute() = 0;
58+
virtual ~Executable_impl() {}
5859
};
5960

6061
} // internal

0 commit comments

Comments
 (0)