[webkit-dev] PSA: Don't try to hold onto temporaries with references

Darin Adler darin at apple.com
Sun Oct 3 10:31:39 PDT 2010

On Oct 3, 2010, at 2:21 AM, Adam Barth wrote:

> If a function returns a temporary, you probably don't want to hold onto it with a "const Foo& foo".  The temporary will get deallocated
> and then you'll be left with a reference to dead memory, which is bad new bears:

I don’t understand why the crashes were happening here and why your code changes were helpful.

What you say here about object lifetime is not correct. I thought the same thing a year or so back. But the C++ language keeps these objects alive until the end of the block. Some other programmers on the project challenged me when I made this assertion, and we found that I was wrong and they were right.

But that doesn’t explain why the code was crashing!

    -- Darin

More information about the webkit-dev mailing list