Strange behavior on shrinking a Dynamic Array -- Capacity reduces to 0

dsimcha dsimcha at yahoo.com
Thu Jun 23 08:54:24 PDT 2011


== Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
> On Thu, 23 Jun 2011 00:15:12 -0400, d coder <dlang.coder at gmail.com> wrote:
> >>
> >>
> >> Yes. Capacity is only non-zero for arrays which can safely be extended.
> >> If
> >> you shrink an array from length 10 to length 5, the GC is smart enough
> >> to
> >> know that that there may be a dangling reference to elements [5..10] and
> >> thus if the length 5 array was appended to, it might stomp on someone
> >> else's
> >> data. Long ago, the GC was dumber and it caused a massive hole in the
> >> const/immutable system. If you know that no other aliases exist (i.e.
> >> you're
> >> buffering, etc) you can use the function assumeSafeAppend to reset the
> >> capacity. However, I'd strongly recommend switching to Appender whenever
> >> you're tempted to use assumeSafeAppend.
> >>
> >
> > Thanks All
> >
> > I would give Appender a try. Any idea if it is safe to use this struct in
> > multithreaded environment. Would it be OK to add elements to it from
> > multiple threads -- of course I would be using synchronized code to make
> > sure that only one element gets added at a time. Any other precautions?
> David Simcha has removed Appender from std.parallelism, due to a perceived
> failure during multithreaded appends (even with locking).  I don't see why
> it would happen, but you may want to be cautious.
> See this commit:
>
https://github.com/D-Programming-Language/phobos/commit/5a3761baac147ff43f4dc2ce264a18ad5e4330bd#std/parallelism.d
> -Steve

I don't either.  It was one of those things where I just didn't know what else to
try.  I'm not sure if removing Appender did anything.  It seems to have removed
one manifestation of the issues with std.parallelism, but occasionally it still
segfaults on FreeBSD64.  The bug appears to be in initialization code.  Running
the unittest in a loop 10,000 times by putting a for loop around the unittest code
passes.  Running the unittest several times by restarting the executable
eventually fails.  IIRC, though, running in a for loop used to fail before I took
out the Appender.  I can't reproduce any of this stuff anywhere except the
FreeBSD64 auto tester.


More information about the Digitalmars-d mailing list