Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fb-cpp/Attachment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void Attachment::disconnectOrDrop(bool drop)
else
handle->detach(&statusWrapper);

handle.reset();
handle.release();
}


Expand Down
2 changes: 1 addition & 1 deletion src/fb-cpp/Batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void Batch::close()
{
assert(isValid());
handle->close(&statusWrapper);
handle.reset();
handle.release();
}

FbRef<fb::IMessageMetadata> Batch::getInputMetadata()
Expand Down
4 changes: 2 additions & 2 deletions src/fb-cpp/Blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ void Blob::cancel()
assert(isValid());

handle->cancel(&statusWrapper);
handle.reset();
handle.release();
}

void Blob::close()
{
assert(isValid());

handle->close(&statusWrapper);
handle.reset();
handle.release();
}
3 changes: 2 additions & 1 deletion src/fb-cpp/EventListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void EventListener::cancelEventsHandle()

{ // scope
std::lock_guard mutexGuard{mutex};
handle = eventsHandle;
handle = std::move(eventsHandle);
}

if (!handle)
Expand All @@ -352,4 +352,5 @@ void EventListener::cancelEventsHandle()
StatusWrapper statusWrapper{client};

handle->cancel(&statusWrapper);
handle.release();
}
2 changes: 1 addition & 1 deletion src/fb-cpp/ServiceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,5 @@ void ServiceManager::detachHandle()

StatusWrapper statusWrapper{*client};
handle->detach(&statusWrapper);
handle.reset();
handle.release();
}
13 changes: 12 additions & 1 deletion src/fb-cpp/SmartPtrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ namespace fbcpp
return FbUniquePtr<T>{obj};
}

// FIXME: Review every usage to see if is not leaking one reference count.
///
/// Reference-counted smart pointer for Firebird objects using addRef/release semantics.
///
Expand Down Expand Up @@ -123,6 +122,18 @@ namespace fbcpp
assign(p, false);
}

///
/// Relinquishes ownership of the pointer without releasing it and returns it.
/// Used after Firebird calls that already release the interface on success, like
/// `ITransaction::commit()` or `IAttachment::detach()`.
///
T* release() noexcept
{
T* tmp = ptr;
ptr = nullptr;
return tmp;
}

FbRef& operator=(FbRef& r) noexcept
{
assign(r.ptr, true);
Expand Down
6 changes: 3 additions & 3 deletions src/fb-cpp/Statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ void Statement::free()
if (resultSetHandle)
{
resultSetHandle->close(&statusWrapper);
resultSetHandle.reset();
resultSetHandle.release();
}

currentRow = false;
statementHandle->free(&statusWrapper);
statementHandle.reset();
statementHandle.release();
}

std::string Statement::getLegacyPlan()
Expand All @@ -268,7 +268,7 @@ bool Statement::execute(Transaction& transaction)
if (resultSetHandle)
{
resultSetHandle->close(&statusWrapper);
resultSetHandle.reset();
resultSetHandle.release();
}

currentRow = false;
Expand Down
4 changes: 2 additions & 2 deletions src/fb-cpp/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void Transaction::rollback()
StatusWrapper statusWrapper{client};

handle->rollback(&statusWrapper);
handle.reset();
handle.release();
state = TransactionState::ROLLED_BACK;
}

Expand All @@ -215,7 +215,7 @@ void Transaction::commit()
StatusWrapper statusWrapper{client};

handle->commit(&statusWrapper);
handle.reset();
handle.release();
state = TransactionState::COMMITTED;
}

Expand Down
2 changes: 1 addition & 1 deletion src/fb-cpp/request/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace fbcpp::request
assert(isValid());

handle->free(&statusWrapper);
handle.reset();
handle.release();
}

void Request::start(Transaction& transaction, unsigned level)
Expand Down
148 changes: 148 additions & 0 deletions src/test/HandleRelease.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* MIT License
*
* Copyright (c) 2026 Adriano dos Santos Fernandes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "TestUtil.h"
#include "fb-cpp/Blob.h"
#include "fb-cpp/ServiceManager.h"
#include "fb-cpp/Statement.h"
#include "fb-cpp/Transaction.h"


BOOST_AUTO_TEST_SUITE(HandleReleaseSuite)

namespace
{
// Holds two extra references to a Firebird interface around a call that ends its life.
// released() returns how many references the call gave back and drops the extra ones.
class ReleaseProbe final
{
public:
// Takes the temporary reference returned by getHandle() methods and drops it before counting.
template <typename T>
explicit ReleaseProbe(FbRef<T> handle)
: ptr{handle.get()}
{
handle.reset();

ptr->addRef();
before = ptr->release();
ptr->addRef();
ptr->addRef();
}

public:
int released()
{
const int after = ptr->release();

if (after > 0)
ptr->release();

return before + 1 - after;
}

private:
fb::IReferenceCounted* ptr;
int before = 0;
};

ServiceManagerOptions makeServiceManagerOptions()
{
auto options = ServiceManagerOptions{};

if (const auto server = getServer())
options.setServer(server.value());

return options;
}
} // namespace

BOOST_AUTO_TEST_CASE(handlesReleasedOnce)
{
const auto database = getTempFile("HandleRelease-handlesReleasedOnce.fdb");

Attachment attachment{getClient(), database, AttachmentOptions().setCreateDatabase(true).setForcedWrites(false)};

{ // scope
Transaction transaction{attachment};
ReleaseProbe probe{transaction.getHandle()};
transaction.commit();
BOOST_CHECK_EQUAL(probe.released(), 1);
}

{ // scope
Transaction transaction{attachment};
ReleaseProbe probe{transaction.getHandle()};
transaction.rollback();
BOOST_CHECK_EQUAL(probe.released(), 1);
}

{ // scope
Transaction transaction{attachment};
Statement statement{attachment, transaction, "select 1 from rdb$database"};

statement.execute(transaction);
ReleaseProbe reexecutedResultSet{statement.getResultSetHandle()};
statement.execute(transaction);
BOOST_CHECK_EQUAL(reexecutedResultSet.released(), 1);

ReleaseProbe resultSet{statement.getResultSetHandle()};
ReleaseProbe stmt{statement.getStatementHandle()};
statement.free();
BOOST_CHECK_EQUAL(resultSet.released(), 1);
BOOST_CHECK_EQUAL(stmt.released(), 1);

Blob closedBlob{attachment, transaction};
ReleaseProbe closedBlobProbe{closedBlob.getHandle()};
closedBlob.close();
BOOST_CHECK_EQUAL(closedBlobProbe.released(), 1);

Blob cancelledBlob{attachment, transaction};
ReleaseProbe cancelledBlobProbe{cancelledBlob.getHandle()};
cancelledBlob.cancel();
BOOST_CHECK_EQUAL(cancelledBlobProbe.released(), 1);

transaction.commit();
}

{ // scope
Attachment second{getClient(), database};
ReleaseProbe probe{second.getHandle()};
second.disconnect();
BOOST_CHECK_EQUAL(probe.released(), 1);
}

{ // scope
ServiceManager manager{getClient(), makeServiceManagerOptions()};
ReleaseProbe probe{manager.getHandle()};
manager.disconnect();
BOOST_CHECK_EQUAL(probe.released(), 1);
}

ReleaseProbe probe{attachment.getHandle()};
attachment.dropDatabase();
BOOST_CHECK_EQUAL(probe.released(), 1);
}

BOOST_AUTO_TEST_SUITE_END()
Loading