buffer objects (C-API)

Carl Banks pavlovevidence at gmail.com
Wed May 12 23:26:59 EDT 2010


On May 12, 7:33 pm, moerchendiser2k3 <googler.
1.webmas... at spamgourmet.com> wrote:
> Hi at all,
>
> is it possible that a buffer object deallocates the memory when the
> object is destroyed? I want to wrap the buffer object around some
> memory. Or is there any chance that the buffer object copies the
> memory so it will be responsible when it will be destroyed?


Objects that support the buffer protocol will only deallocate their
memory if they're programmed to.

If you're writing your own buffer type you can be assured the memory
won't be deallocated unless you do it yourself.

If you're referring to the Python 2.x "buffer" object, no it won't
deallocate the memory.  You should also be aware that it's deprecated
in 2.x and removed in 3.x.

Generally speaking, objects that allocate their own memory should also
deallocate it; objects that are passed pointers to existing memory
should leave it alone.  Most built-in types in Python that support
buffer protocol do allocate their own memory, so they also deallocate
it.  "buffer" doesn't, so it doesn't.

One other thing to consider is numpy, especially if your buffer is
numeric.  It might even have the operations you need already.
numpy.ndarray can allocate its own memory or accept a pointer (or
other buffer object), and will only delete the buffer if it allocated
it itself.


Carl Banks



More information about the Python-list mailing list