Thread overview
C++ library says to delete data that it allocates
Sep 22, 2013
Charles Hixson
Sep 22, 2013
w0rp
Sep 22, 2013
Jonathan M Davis
September 22, 2013
I'm trying to use a C++ library that allocates data and returns a pointer to it.  It tells me that I should delete the data when I'm through with it.

Can I do this from within D?  Or do I need to write some C++ code to manage the delete, and pass the pointer on to it?

-- 
Charles Hixson

September 22, 2013
On Sunday, 22 September 2013 at 23:09:52 UTC, Charles Hixson
wrote:
> I'm trying to use a C++ library that allocates data and returns a pointer to it.  It tells me that I should delete the data when I'm through with it.
>
> Can I do this from within D?  Or do I need to write some C++ code to manage the delete, and pass the pointer on to it?

You can't use delete straight from D, so I'd write the delete in
C++ and pass the pointer back to C++, like you say.
September 22, 2013
On Monday, September 23, 2013 01:12:55 w0rp wrote:
> On Sunday, 22 September 2013 at 23:09:52 UTC, Charles Hixson
> 
> wrote:
> > I'm trying to use a C++ library that allocates data and returns a pointer to it.  It tells me that I should delete the data when I'm through with it.
> > 
> > Can I do this from within D?  Or do I need to write some C++ code to manage the delete, and pass the pointer on to it?
> 
> You can't use delete straight from D, so I'd write the delete in C++ and pass the pointer back to C++, like you say.

Yeah. Memory needs to be freed by the same allocator that allocated it, as that's the allocator that manages it (e.g. if C's malloc allocated the memory, then C's free must free it, or if C++'s new allocated it, then C++'s delete must free it). So, if C++ code allocated something, it's going to have to be C++ code which frees it (though that could be managed by some kind of smart pointer in D which called a C++ function to free the memory when the smart pointer determined that the memory needed to be freed).

- Jonathan M Davis