Home

CoTaskMemFree

CoTaskMemFree is a Windows API function used to release memory that was allocated by the COM task memory allocator via CoTaskMemAlloc. It is part of the Component Object Model (COM) memory management model and is designed to support memory handoffs across component and process boundaries.

The function has the prototype void CoTaskMemFree(void* pv); it does not return a value. If the provided

Usage typically arises when a COM API or a system function returns memory allocated with CoTaskMemAlloc. The

Related functions include CoTaskMemAlloc (to allocate memory), CoTaskMemSize (to query the size of memory allocated with

pointer
pv
is
NULL,
the
call
has
no
effect.
Memory
allocated
with
CoTaskMemAlloc
should
be
freed
with
CoTaskMemFree;
do
not
use
general
purpose
allocators
such
as
HeapFree,
LocalFree,
or
GlobalFree
to
release
it.
Using
the
wrong
allocator
can
lead
to
leaks
or
heap
corruption.
caller
is
responsible
for
freeing
that
memory
when
it
is
no
longer
needed,
after
which
the
memory
is
reclaimed
by
the
COM
task
memory
allocator.
CoTaskMemFree
is
safe
to
call
across
thread
and
apartment
boundaries,
but
the
pointer
must
have
been
allocated
with
CoTaskMemAlloc.
CoTaskMemAlloc),
and
general
COM
memory
management
guidelines.
CoTaskMemFree
is
implemented
by
the
COM
runtime
(often
associated
with
Ole32/COM
support
libraries)
and
is
a
standard
mechanism
for
managing
memory
across
COM
boundaries.