"Error: address of variable this assigned to this with longer lifetime"

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Mar 14 14:58:42 UTC 2018


On Wednesday, March 14, 2018 07:11:49 Nathan S. via Digitalmars-d-learn 
wrote:
> On Tuesday, 13 March 2018 at 22:33:56 UTC, Jonathan M Davis wrote:
> > And you can't get rid of it, because the object can still be
> > moved, which would invalidate the pointer that you have
> > referring to the static array.
> > ...
> > https://issues.dlang.org/show_bug.cgi?id=17448
>
> Thanks for the info.

Another way to tackle this problem is to not slice the static array but to
keep track of either two indices or an index and the length explicitly and
then use those with the static array. It's more annoying in some respects,
but it solves the @safety problem.

LOL. It was actually thanks to your post here that it clicked for me that I
had this problem with some of my recent code. I was well aware of the
problems with having pointers to structs on the stack, but it hadn't clicked
that slicing a static array like that was the same thing until you posted
about it. I had realized that having a dynamic array in the struct being a
slice of a static array in the struct would cause problems when the struct
was copied, and I'd dealt with that with a postblit constructor, but the
issue with moving didn't click until I connected the dots between your post
and that recent bugzilla issue. So, answering your question actually helped
me catch a bug in my code.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list