google / google/j2objc

Protobuf memory leaks - SINGULAR_SETTER_IMP

Open
#2,410 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
6k
Forks
999
Avg merge
19h 20m
Merged PRs (30d)
14

Description

Hi,

we finally decided to update j2objc from very old version 2.8 to current master. During our profiling tests we realised, that after update, there is many memory leaks related to protobuffers. Because we are using protobuffers for inter device communication and we are sending relative big amount of data through them, this is major issue for us.

I went backwards through all changes in protobuf runtime, until I found change, that was implemented in commit 9e9614f . Specifically, the macro:

```objective-c
#define SINGULAR_SETTER_IMP(NAME) \
static void SingularSet##NAME(id msg, TYPE_##NAME value, size_t offset, CGPHasLocator hasLoc) { \
TYPE_##NAME *ptr = FIELD_PTR(TYPE_##NAME, msg, offset); \
ClearPreviousOneof(msg, hasLoc, (uintptr_t)ptr); \
TYPE_ASSIGN_##NAME(*ptr, value); \
SetHas(msg, hasLoc); \
}
```

that was changed in this commit to:
```objective-c
#define SINGULAR_SETTER_IMP(NAME) \
static void SingularSet##NAME(id msg, TYPE_##NAME value, size_t offset, CGPHasLocator hasLoc) { \
TYPE_##NAME *ptr = FIELD_PTR(TYPE_##NAME, msg, offset); \
ClearPreviousOneof(msg, hasLoc, (uintptr_t)ptr); \
TYPE_RETAINED_ASSIGN_##NAME(*ptr, value); \
SetHas(msg, hasLoc); \
}
```

seems to causing the memory leaks.

When I changed `TYPE_RETAINED_ASSIGN` back to `TYPE_ASSIGN`, memory leaks disappeared.

The screenshot from profiler:

![Image](https://github.com/user-attachments/assets/31ccf2a3-6373-4d42-8083-f6dceed589a2)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.