free causes exception
Mike Parker via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 26 16:59:30 PST 2016
On Tuesday, 26 January 2016 at 21:23:28 UTC, Igor wrote:
>
> um? Memory manager? I am doing it manually C++ style so I don't
> have to worry about the god forsaken memory manager. Why is it
> so difficult? I create the object and release it when I need to.
He's talking about *your* memory manager, whatever system you
have set up to allocate and deallocate memory.
>
> I can replace the destroy(f) with free(inline the code) but I
> don't see why that should matter. The whole point of
> destructors is to do this sort of stuff. That's why they were
> invented in the first place!?!
Not in D! You have to get your mind out of C++ mode when
programming in D. D is not C++, no matter how similar they are,
and there are idioms that work well in C++ that do not work in D.
There are cases where D's destructors behave like those in C++,
but not always.
Since this approach is failing for you, I suggest you make a
function or template that can take any object you've manually
allocated, call destroy on it, then deallocate it. The destructor
can still clean up any resources the object maintains, but the
responsibility for deallocating the object will be taken out of
the destructor. It also ensures that deallocation does not
interfere with the operation of destroy.
More information about the Digitalmars-d-learn
mailing list