Skip to content

Commit 5514ea8

Browse files
committed
fix(formats/protobuf): Don't mix new and free
Signed-off-by: Philipp Jungkamp <philipp.jungkamp@rwth-aachen.de>
1 parent 893caa4 commit 5514ea8

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

lib/formats/protobuf.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,23 @@ int ProtobufFormat::sprint(char *buf, size_t len, size_t *wbytes,
4040
const struct Sample *const smps[], unsigned cnt) {
4141
unsigned psz;
4242

43-
auto *pb_msg = new Villas__Node__Message;
43+
auto *pb_msg = reinterpret_cast<Villas__Node__Message *>(
44+
calloc(1, sizeof(Villas__Node__Message)));
4445
if (!pb_msg)
4546
throw MemoryAllocationError();
4647

4748
villas__node__message__init(pb_msg);
4849

4950
pb_msg->n_samples = cnt;
50-
pb_msg->samples = new Villas__Node__Sample *[pb_msg->n_samples];
51+
pb_msg->samples = reinterpret_cast<Villas__Node__Sample **>(
52+
calloc(pb_msg->n_samples, sizeof(Villas__Node__Sample *)));
5153
if (!pb_msg->samples)
5254
throw MemoryAllocationError();
5355

5456
for (unsigned i = 0; i < pb_msg->n_samples; i++) {
5557
Villas__Node__Sample *pb_smp = pb_msg->samples[i] =
56-
new Villas__Node__Sample;
58+
reinterpret_cast<Villas__Node__Sample *>(
59+
calloc(1, sizeof(Villas__Node__Sample)));
5760
if (!pb_msg->samples[i])
5861
throw MemoryAllocationError();
5962

@@ -69,7 +72,8 @@ int ProtobufFormat::sprint(char *buf, size_t len, size_t *wbytes,
6972
}
7073

7174
if (flags & smp->flags & (int)SampleFlags::HAS_TS_ORIGIN) {
72-
pb_smp->ts_origin = new Villas__Node__Timestamp;
75+
pb_smp->ts_origin = reinterpret_cast<Villas__Node__Timestamp *>(
76+
calloc(1, sizeof(Villas__Node__Timestamp)));
7377
if (!pb_smp->ts_origin)
7478
throw MemoryAllocationError();
7579

@@ -80,7 +84,8 @@ int ProtobufFormat::sprint(char *buf, size_t len, size_t *wbytes,
8084
}
8185

8286
pb_smp->n_values = smp->length;
83-
pb_smp->values = new Villas__Node__Value *[pb_smp->n_values];
87+
pb_smp->values = reinterpret_cast<Villas__Node__Value **>(
88+
calloc(pb_smp->n_values, sizeof(Villas__Node__Value *)));
8489
if (!pb_smp->values)
8590
throw MemoryAllocationError();
8691

@@ -90,7 +95,9 @@ int ProtobufFormat::sprint(char *buf, size_t len, size_t *wbytes,
9095
}
9196

9297
for (unsigned j = 0; j < pb_smp->n_values; j++) {
93-
Villas__Node__Value *pb_val = pb_smp->values[j] = new Villas__Node__Value;
98+
Villas__Node__Value *pb_val = pb_smp->values[j] =
99+
reinterpret_cast<Villas__Node__Value *>(
100+
calloc(1, sizeof(Villas__Node__Value)));
94101
if (!pb_val)
95102
throw MemoryAllocationError();
96103

0 commit comments

Comments
 (0)