OCILIB Tutorials v5.0.0
Beginner

How the C++ API manages handles

Objectives — by the end of this page you will be able to:

From manual frees to shared ownership

In the C API every handle follows the rule from the architecture page: whoever creates it frees it, exactly once, before its parent goes away. The C++ API keeps the same ownership tree but takes the bookkeeping off your hands — there is no Free() to call, and leaving scope is enough:

{
    ocilib::Environment env;
    ocilib::Connection  con(env, db, usr, pwd);
    ocilib::Statement   stmt(con);
}   // stmt's OCI_Statement, con's OCI_Connection and env's OCI_Environment
    // are freed here — in reverse construction order, for free

What makes it work is that each wrapper does not own the handle directly — it owns a share of a small control block that knows how to free it.

The layers under every C++ object

ocilib::Connection ─┐
ocilib::Connection ─┼─ shared_ptr ──► HandleControl ──► OCI_Connection*
   (copies)         │                    │  raw OCI handle (atomic)
                    │                    │  owned or borrowed flag
                    │                    │  parent / children links
                    │                    │  free-notification hook
                    └─ the last shared_ptr out triggers the free

Copying a C++ object copies the share, not the handle. Two Connection variables hold one OCI_Connection; the handle is freed when the last copy is released — which is why passing wrappers by value to functions is safe.

Owned vs borrowed handles

Not every wrapper frees its handle — the control block records whether the C++ side created the object or merely observed it:

Kind Created when Freed by the wrapper?
Owned (AcquireAllocated) the wrapper ran OCI_*Create/OCI_*Get-style construction — Connection(env, …), Statement(con) yes — on last release
Borrowed (AcquireTransient) the handle came from another object — Column of a resultset, TypeInfo from a describe, Resultset from a statement, or a constructor taking a raw OCI_* pointer no — its owner manages it

Borrowed handles mirror the C rule "don't free what you didn't create" — the wrapper stays valid only while its owner lives.

The handle store — one control block per OCI handle

Each ocilib::Environment keeps a registry (the HandleStore) mapping raw OCI_* handles to their control blocks. When the C++ layer wraps a handle it consults the store first:

Environments must come from the C++ API

Deduplication requires the OCI_Environment to have been created through ocilib::Environment — the C++ object registers itself so any raw handle can find its store. Wrapping handles that belong to a C-created environment (OCI_EnvironmentCreate) is unsupported and can double-free.

The parent/child tree

The C ownership tree exists in C++ too — every control block links to its parent and tracks live children (Environment → Connection → Statement → Resultset):

Freeing is idempotent and thread-safe: the raw handle is swapped out atomically, so the first Free()/Close() wins and racing wrappers see a no-op — no double-free even under OCI_ENV_THREADED.

Explicit release — Close() and friends

Automatic release happens at scope exit, but Oracle-side resources are real (server sessions, cursors, latches). To give one up early, call the wrapper's explicit close — it drops that wrapper's share; the actual OCI free still waits for the last sharer:

ocilib::Connection con(env, db, usr, pwd);
ocilib::Connection alsoCon = con;   // same OCI_Connection

con.Close();                        // drops con's share — still open!
alsoCon.Close();                    // last share out → OCI_ConnectionFree now

Close() exists on Connection, Lob, File, Pool, …; a closed wrapper reads as false in if (wrapper) and can be re-Opened/re-acquired.

Try it yourself

  1. Copy a Connection into a second variable, Close() the first — run a statement through the second: still connected, because the last sharer holds the handle.
  2. Call stmt.GetConnection() twice and compare with == — same underlying control block, courtesy of the handle store.
  3. Let a Resultset outlive… nothing — observe it becomes unusable after its Statement is released: borrowed handles don't own their parents.

Common pitfalls

Recap

Next: when things go wrong — the error handling page.