# StringRef life cycle

**URL:** https://forums.foundationdb.org/t/stringref-life-cycle/2986
**Category:** FoundationDB Core
**Created:** [November 2, 2021, 1:10pm UTC](https://forums.foundationdb.org/t/stringref-life-cycle/2986 "2021-11-02T13:10:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![superhail](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/superhail/32/1191_2.png) [@superhail](https://forums.foundationdb.org/u/superhail)
#### Post date: [November 2, 2021, 1:10pm UTC](https://forums.foundationdb.org/t/stringref-life-cycle/2986/1 "2021-11-02T13:10:00Z")

</div>

`StringRef` can be sent in reply as a parameter, like below, but StringRef does not manage its memory directly, So where is it managed?(It will be helpful if you can tell me where the corresponding codes are)

```auto
struct TLogPeekReply {
	constexpr static FileIdentifier file_identifier = 11365689;
	Arena arena;
	StringRef messages;
	Version end;
	Optional<Version> popped;
	Version maxKnownVersion;
	Version minKnownCommittedVersion;
	Optional<Version> begin;
	bool onlySpilled = false;

	template <class Ar>
	void serialize(Ar& ar) {
		serializer(ar, arena, messages, end, popped, maxKnownVersion, minKnownCommittedVersion, begin, onlySpilled);
	}
};

```

---

<div class="post-metadata">

### Author: ![andrew.noyes](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/andrew.noyes/32/443_2.png) [@andrew.noyes](https://forums.foundationdb.org/u/andrew.noyes)
#### Post date: [November 2, 2021, 5:47pm UTC](https://forums.foundationdb.org/t/stringref-life-cycle/2986/2 "2021-11-02T17:47:07Z")

</div>

Logically `TLogPeekReply::arena` owns the memory referenced by `TLogPeekReply::messages`, but there’s actually a bug (that apparently doesn’t manifest) here. `arena` needs to appear after `messages` in the call to `serializer` to ensure that `arena` really does own the memory. This requirement is described briefly here: [foundationdb/flow at master · apple/foundationdb · GitHub](https://github.com/apple/foundationdb/tree/master/flow#flatbuffersobjectserializer).

[foundationdb/Arena.h at 37bc41abbbff8626889e75b98d1d20d5476d1900 · apple/foundationdb · GitHub](https://github.com/apple/foundationdb/blob/37bc41abbbff8626889e75b98d1d20d5476d1900/flow/Arena.h#L128) is where the arena would take ownership of the memory referenced by StringRef if the arguments to `serializer` were ordered correctly.

That’s all mostly only relevant for using `TLogPeekReply` deserialized from a network message though. If you are constructing `TLogPeekReply` in order to send it it’s your responsibility to make sure that `arena` owns the underlying memory.

---

<div class="post-metadata">

### Author: ![superhail](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/superhail/32/1191_2.png) [@superhail](https://forums.foundationdb.org/u/superhail)
#### Post date: [November 3, 2021, 3:24am UTC](https://forums.foundationdb.org/t/stringref-life-cycle/2986/3 "2021-11-03T03:24:51Z")

</div>

What if this reply has no arena field? In that case, who will manage the memory for messages? @andrew.noyes

---

<div class="post-metadata">

### Author: ![andrew.noyes](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/andrew.noyes/32/443_2.png) [@andrew.noyes](https://forums.foundationdb.org/u/andrew.noyes)
#### Post date: [November 3, 2021, 5:16pm UTC](https://forums.foundationdb.org/t/stringref-life-cycle/2986/4 "2021-11-03T17:16:06Z")

</div>

If the reply has a `StringRef` field and no `Arena` field, then the underlying memory for the `StringRef` doesn’t have an owner tied to the reply. Logically that’s a bug or at least highly unconventional. There are various unreliable ways that the memory might be kept alive long enough.

Usually for replies without Arenas we’d either have a `Standalone<StringRef>` or `std::string` instead.

---

<div class="post-metadata">

### Author: ![Syaifulnizam](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/syaifulnizam/32/1222_2.png) [@Syaifulnizam](https://forums.foundationdb.org/u/Syaifulnizam)
#### Post date: [November 10, 2021, 2:00am UTC](https://forums.foundationdb.org/t/stringref-life-cycle/2986/5 "2021-11-10T02:00:56Z")

</div>

Thanks,approve this file
