deprecated delete and manual memory management
Steven Schveighoffer
schveiguy at yahoo.com
Tue Apr 26 17:03:25 PDT 2011
On Tue, 26 Apr 2011 19:45:21 -0400, Daniel Gibson <metalcaedes at gmail.com>
wrote:
> I just noticed something regarding clear():
> struct Bar { ... }
> Bar *b = new Bar();
> This compiles without any warning, but doesn't call the destructor:
> clear(b);
> This compiles without any warning and actually does what you'd expect,
> i.e. calls destructor:
> clear(*b);
>
> Is this behavior expected/wanted? If not: Is it a known bug? I couldn't
> find it in the bugtracker, but maybe I searched for the wrong keywords.
Let's start with class references. Because class references cannot be
separated from its reference, you have to finalize the class when
finalizing the reference, because there's no way to say "clear what this
reference refers to" vs. "clear this reference". So you have to give a
way to finalize a class instance.
With pointers, however, you can specify as you say, whether you want to
clear the pointer or the struct itself.
Now, is it much useful to clear a pointer without clearing what it points
to? I'd say no, clearing a pointer is as easy as doing ptr = null. So
I'm thinking, it should be filed as a bug.
The obvious thing to decide is, what should be done on references to
references? If you clear a double pointer, should it go through both
pointers? Or a pointer to a class reference?
I'd say no, but you have to take extra steps to ensure it is this way.
-Steve
More information about the Digitalmars-d
mailing list