Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cyclonedds-dev] Segmentation fault

Hi Giovanni,

There is always a bit of guesswork involved with a description of the code that fails instead of a reproducer, but if I understand correctly, then you’re doing something like:

void *sample[MAX_SAMPLE] = { 0 };
dds_sample_info_t infos[MAX_SAMPLE];
int nsample = 0
while ((nsample = dds_read ( reader, &sample[nsample] , &infos[nsample], MAX_SAMPLE, MAX_SAMPLE)) > 0) {
  dds_return_loan (reader, sample, nsample);

But that means on the second and later iterations of the loop, you’re providing space for only (MAX_SAMPLE - nsample) samples (where nsample > 0 because of the loop condition) rather than space for MAX_SAMPLE samples. That can easily lead to a crash. Instead you need “&sample[0]” and “&infos[0]” (or, equivalently, “sample” and “infos”).

Another possibility is that you didn’t set up the pointers in the “sample” array correctly. If the first element is a null pointer, it’ll allocate memory for you, which you then have to release (hence why I put in the dds_return_loan). You can also make them point to actual samples, like:

T sample[MAX_SAMPLE];
memset (sample, 0, sizeof (sample)); // only needed if there are strings/sequences in T
void *sample_ptrs[MAX_SAMPLE];
for (size_t i = 0; i < MAX_SAMPLE; i++) {
  sample_ptrs[i] = &sample[i];
dds_sample_info_t infos[MAX_SAMPLE];
int nsample = 0
while ((nsample = dds_read ( reader, sample_ptrs, infos, MAX_SAMPLE, MAX_SAMPLE)) > 0) {

If there are strings or sequences in T, then I’m afraid you still have to free memory in sample[…], I’m afraid.

Just some guesses …

On a side note I generally prefer GitHub for issues/questions, but as you can see the -dev mailinglist does work, too :)

Best regards,

On 21 Mar 2023, at 15:42, giovanni viglialoro via cyclonedds-dev <cyclonedds-dev@xxxxxxxxxxx> wrote:

Good morning,
I am developing a system based on cycloneDDS in c but I am facing a problem. I have several message sent by a publisher but I cannot receive on subscriber side anything different from a the first message sent. I was expecting that (based on the hello world example that setting MAX SAMPLE to 4 and set up the while in order to dds_read ( reader, &sample[nsample] , &infos[nsample], MAX_SAMPLE,MAX_SAMPLE) would be sufficient..  but it gave a segmentation fault. Is there someone could help?

cyclonedds-dev mailing list
To unsubscribe from this list, visit

Back to the top