Skip to content

Commit

Permalink
fix: SerializedProto's memory leaks (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Feb 22, 2022
1 parent 0dafc8a commit ac6316d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
namespace Mediapipe
{
[StructLayout(LayoutKind.Sequential)]
internal struct SerializedProto
internal readonly struct SerializedProto
{
public IntPtr str;
public int length;
private readonly IntPtr _str;
private readonly int _length;

public void Dispose()
{
UnsafeNativeMethods.delete_array__PKc(str);
UnsafeNativeMethods.delete_array__PKc(_str);
}

public T Deserialize<T>(pb::MessageParser<T> parser) where T : pb::IMessage<T>
{
var bytes = new byte[length];
Marshal.Copy(str, bytes, 0, bytes.Length);
var bytes = new byte[_length];
Marshal.Copy(_str, bytes, 0, bytes.Length);
return parser.ParseFrom(bytes);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
namespace Mediapipe
{
[StructLayout(LayoutKind.Sequential)]
internal struct SerializedProtoVector
internal readonly struct SerializedProtoVector
{
public IntPtr data;
public int size;
private readonly IntPtr _data;
private readonly int _size;

public void Dispose()
{
UnsafeNativeMethods.mp_api_SerializedProtoArray__delete(data);
UnsafeNativeMethods.mp_api_SerializedProtoArray__delete(_data, _size);
}

public List<T> Deserialize<T>(pb::MessageParser<T> parser) where T : pb::IMessage<T>
{
var protos = new List<T>(size);
var protos = new List<T>(_size);

unsafe
{
var protoPtr = (SerializedProto*)data;
var protoPtr = (SerializedProto*)_data;

for (var i = 0; i < size; i++)
for (var i = 0; i < _size; i++)
{
var serializedProto = Marshal.PtrToStructure<SerializedProto>((IntPtr)protoPtr++);
protos.Add(serializedProto.Deserialize(parser));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static partial class UnsafeNativeMethods
[MarshalAs(UnmanagedType.FunctionPtr)] Protobuf.ProtobufLogHandler logHandler);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern void mp_api_SerializedProtoArray__delete(IntPtr serializedProtoVectorData);
public static extern void mp_api_SerializedProtoArray__delete(IntPtr serializedProtoVectorData, int size);

#region MessageProto
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
Expand Down
8 changes: 7 additions & 1 deletion mediapipe_api/external/protobuf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ MpReturnCode google_protobuf__SetLogHandler__PF(LogHandler* handler) {
CATCH_EXCEPTION
}

void mp_api_SerializedProtoArray__delete(mp_api::SerializedProto* serialized_proto_vector_data) { delete[] serialized_proto_vector_data; }
void mp_api_SerializedProtoArray__delete(mp_api::SerializedProto* serialized_proto_vector_data, int size) {
auto serialized_proto = serialized_proto_vector_data;
for (auto i = 0; i < size; ++i) {
delete (serialized_proto++)->str;
}
delete[] serialized_proto_vector_data;
}
2 changes: 1 addition & 1 deletion mediapipe_api/external/protobuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ typedef void LogHandler(int level, const char* filename, int line, const char* m

MP_CAPI(MpReturnCode) google_protobuf__SetLogHandler__PF(LogHandler* handler);

MP_CAPI(void) mp_api_SerializedProtoArray__delete(mp_api::SerializedProto* serialized_proto_vector_data);
MP_CAPI(void) mp_api_SerializedProtoArray__delete(mp_api::SerializedProto* serialized_proto_vector_data, int size);

} // extern "C"

Expand Down

0 comments on commit ac6316d

Please sign in to comment.