When could GC move memory?

9 views
Skip to first unread message

Koji Ishii

unread,
Feb 13, 2025, 1:47:32 AMFeb 13
to memory-dev, Omer Katz, Kent Tamura
Hi memory experts,

I'm looking into eliminating raw pointers to traceable DISALLOW_NEW objects (crbug.com/389707047, note its visibility is limited due to possible security risk). While working on it, I heard that memory could move on any memory operations, and I hope you could help me to understand this in more detail to write safe code.

For example:
class Item {
  DISALLOW_NEW();
};
struct Container : public GarbageCollected<Container> {
  HeapVector<Item> items;
};
void Test(const Container& container) {
  for (const auto& item : container.items) {
    auto x = MakeGarbageCollected<X>();  // <-- Could this move memory?
    item.Func();
  }
}
If the `MakeGarbageCollected` could move the backing buffer of the `HeapVector`, `item.Func()` will be a bad memory access.

Is this something I need to worry about, or are there more detailed conditions on when memory could be moved?

Omer Katz (chromium.org)

unread,
Feb 13, 2025, 2:19:44 AMFeb 13
to Koji Ishii, memory-dev, Kent Tamura
No. This is not something you need to worry about and the example code you gave should be fine.

Generally speaking, every call to MakeGarbageCollected could trigger a GC, and every GC could move the HeapVector backing store around.
However, to avoid cases like the one above becoming an issue, if the GC finds any pointers on stack that reference the HeapVector backing store (as it would find in this case), it will not try to move that backing store.
Reply all
Reply to author
Forward
0 new messages