clear bug?

Jonathan M Davis jmdavisProg at gmx.com
Mon Sep 5 19:20:22 PDT 2011


On Monday, September 05, 2011 19:05:56 Dan Olson wrote:
> Jonathan M Davis <jmdavisProg at gmx.com> writes:
> > On Tuesday, September 06, 2011 02:12:11 Andrej Mitrovic wrote:
> >> On 9/6/11, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> >> > Why would you
> >> > ever try and use an object that had been cleared?
> >> 
> >> TDPL, that's why. Things might have changed but how would someone new
> >> to D know that?
> > 
> > I really don't pay much attention to clear, since I rarely use it, and I
> > don't know why you'd ever actually want to try and use an object that
> > you cleared. I do remember discussions about making it clear out the
> > vtable, since you _want_ it to blow up after it's been cleared rather
> > than have an invalid object work on some level. Rereading the relevant
> > section in TDPL though, it does look like it describes a different
> > situation than seems to have been ultimately settled on. So, I guess
> > that I don't know exactly what the situation currently is or what it's
> > supposed to be. But I wouldn't have expected clear to result in a valid
> > object. But clearing out the vtable at least makes it blow up nicely
> > instead of doing who knows what with memory when you try and call
> > functions on an invalid object. So, if the ultimate goal is just to
> > make it memory safe, then both clearing out the vtable and the
> > situation that TDPL describes do the trick, though deleting the vtable
> > actually helps tell you that you've used an object when you shouldn't,
> > whereas TDPL's description results in using the object succeeding,
> > which probably isn't a good thing.
> > 
> > - Jonathan M Davis
> 
> Thanks for the clearification.
> 
> It was based on TDPL 6.3.5 that I thought the idea of clear() was to
> clean up but leave the thing in an initialized state.  And because
> clear() on dynamic arrays and other types seems to be be intended to
> leave you with a valid and initialized object.  Or is that not true?

I don't really know what the current state of clear is. I know that there were 
issues which caused people to want clear to zero-out the vtable on objects, 
and I believe that that's what it currently does. One of the reasons for clear 
appears to be to make it memory-safe - and both putting the object in a 
default state and zeroing out the vtable will do that. It's just that in the 
one case, you can use it (though you probably shouldn't be and its state isn't 
very interesting), while in the other, it will blow up if you use it.

Dynamic arrays don't have vtables, so there's no way that calling clear on 
them would zero-out their vtable. I was unaware that you even could call clear 
on them (I really don't use clear much). I would have guessed that doing so 
would just call clear on each of its elements and then set the array's length 
to 0 (and probably its ptr to null), but I don't know. I really don't use 
clear (it's not generally supposed to be necessary), so I don't know exactly 
what it's state is or how it differs (if at all) from how it's supposed to 
work.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list