Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning about memory leak in tests [21177] (backport #141) #159

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 1 addition & 79 deletions fastdds_python/src/swig/fastdds/dds/core/policy/QosPolicies.i
Original file line number Diff line number Diff line change
Expand Up @@ -58,91 +58,13 @@ namespace dds {
}
}

%inline %{
class OctetResourceLimitedVectorStopIterator {};
class OctetResourceLimitedVectorIterator {
public:
OctetResourceLimitedVectorIterator(
eprosima::fastrtps::ResourceLimitedVector<eprosima::fastrtps::rtps::octet>::iterator _cur,
eprosima::fastrtps::ResourceLimitedVector<eprosima::fastrtps::rtps::octet>::iterator _end) : cur(_cur), end(_end) {}
OctetResourceLimitedVectorIterator* __iter__()
{
return this;
}
eprosima::fastrtps::ResourceLimitedVector<eprosima::fastrtps::rtps::octet>::iterator cur;
eprosima::fastrtps::ResourceLimitedVector<eprosima::fastrtps::rtps::octet>::iterator end;
};
%}

// SWIG does not support templates in the generated binding,
// because not all output languages support them
// We must explicitly declare the specializations of the templates
%template(OctetResourceLimitedVector) eprosima::fastrtps::ResourceLimitedVector<eprosima::fastrtps::rtps::octet>;
resource_limited_vector_template(OctetResourceLimitedVector, eprosima::fastrtps::rtps::octet)

%include "fastdds/dds/core/policy/QosPolicies.hpp"

%include "exception.i"
%exception OctetResourceLimitedVectorIterator::__next__ {
try
{
$action // calls %extend function __next__() below
}
catch (OctetResourceLimitedVectorStopIterator)
{
PyErr_SetString(PyExc_StopIteration, "End of iterator");
return nullptr;
}
}

%extend OctetResourceLimitedVectorIterator
{
eprosima::fastrtps::rtps::octet __next__()
{
if ($self->cur != $self->end)
{
// dereference the iterator and return reference to the object,
// after that it increments the iterator
return *$self->cur++;
}
throw OctetResourceLimitedVectorStopIterator();
}
}

%exception eprosima::fastrtps::ResourceLimitedVector<eprosima::fastrtps::rtps::octet>::__getitem__
{
try
{
$action
}
catch(std::out_of_range)
{
SWIG_exception(SWIG_IndexError, "Index out of bounds");
}
}

%extend eprosima::fastrtps::ResourceLimitedVector<eprosima::fastrtps::rtps::octet>
{
OctetResourceLimitedVectorIterator __iter__()
{
// return a constructed Iterator object
return OctetResourceLimitedVectorIterator($self->begin(), $self->end());
}

size_t __len__() const
{
return self->size();
}

eprosima::fastrtps::rtps::octet __getitem__(int i)
{
if (self->size() <= i)
{
throw std::out_of_range("Index out of bounds");
}
return (*self)[i];
}
}

%exception eprosima::fastdds::dds::PartitionQosPolicy::__getitem__
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "fastrtps/utils/collections/ResourceLimitedVector.hpp"
%}

%include "exception.i"

// Operator[] is ignored by SWIG because it does not map correctly to target languages
// mostly because of its dual getter/setter nature
// We can ignore them and extend to make the getter and setter methods explicit and break the overload
Expand All @@ -29,6 +31,7 @@
%ignore eprosima::fastrtps::ResourceLimitedVector::at;
%ignore eprosima::fastrtps::ResourceLimitedVector::front;
%ignore eprosima::fastrtps::ResourceLimitedVector::back;
%ignore eprosima::fastrtps::ResourceLimitedVector::push_back;

// Initializer lists are note supported in SWIG. Ignore the method
%ignore eprosima::fastrtps::ResourceLimitedVector::assign(std::initializer_list<value_type>);
Expand All @@ -37,28 +40,107 @@
// and SWIG does not support it in any case
%ignore eprosima::fastrtps::ResourceLimitedVector::operator const collection_type&;


%extend eprosima::fastrtps::ResourceLimitedVector {
pointer at(size_type pos)
%exception eprosima::fastdds::ResourceLimitedVector::__getitem__
{
try
{
$action
}
catch(std::out_of_range)
{
return &($self->at(pos));
SWIG_exception(SWIG_IndexError, "Index out of bounds");
}
pointer front()
}


%extend eprosima::fastrtps::ResourceLimitedVector {

size_t __len__() const
{
return &($self->front());
return self->size();
}
pointer back()

value_type __getitem__(int i)
{
return &($self->back());
if (self->size() <= i)
{
throw std::out_of_range("Index out of bounds");
}
return (*self)[i];
}

pointer getitem(size_type n) {
return &($self->operator[](n));
}

void setitem(size_type n, const_pointer v) {
$self->operator[](n) = *v;
void setitem(size_type n, value_type v) {
$self->operator[](n) = v;
}

void append(value_type v) {
$self->push_back(v);
}
}

%include "fastrtps/utils/collections/ResourceLimitedVector.hpp"

%define resource_limited_vector_template(name_, value_type_)
%inline %{
class name_ ## StopIterator {};
class name_ ## Iterator
{
public:
name_ ## Iterator(
typename eprosima::fastrtps::ResourceLimitedVector<value_type_>::iterator _cur,
typename eprosima::fastrtps::ResourceLimitedVector<value_type_>::iterator _end)
: cur(_cur)
, end(_end)
{
}

name_ ## Iterator* __iter__()
{
return this;
}
typename eprosima::fastrtps::ResourceLimitedVector<value_type_>::iterator cur;
typename eprosima::fastrtps::ResourceLimitedVector<value_type_>::iterator end;
};
%}

%exception name_ ## Iterator::__next__ {
try
{
$action // calls %extend function __next__() below
}
catch (name_ ## StopIterator)
{
PyErr_SetString(PyExc_StopIteration, "End of iterator");
return nullptr;
}
}

%extend name_ ## Iterator
{
value_type_ __next__()
{
if ($self->cur != $self->end)
{
// dereference the iterator and return reference to the object,
// after that it increments the iterator
return *$self->cur++;
}
throw name_ ## StopIterator();
}
}

%template(name_) eprosima::fastrtps::ResourceLimitedVector<value_type_>;

%extend eprosima::fastrtps::ResourceLimitedVector<value_type_>
{
name_ ## Iterator __iter__()
{
// return a constructed Iterator object
return name_ ## Iterator($self->begin(), $self->end());
}
}
%enddef
4 changes: 2 additions & 2 deletions fastdds_python/test/api/test_domainparticipant.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ def test_get_set_qos(participant):
"""
qos = fastdds.DomainParticipantQos()
assert(fastdds.ReturnCode_t.RETCODE_OK == participant.get_qos(qos))
qos.user_data().push_back(1)
qos.user_data().push_back(2)
qos.user_data().append(1)
qos.user_data().append(2)
assert(2 == len(qos.user_data()))

assert(fastdds.ReturnCode_t.RETCODE_OK ==
Expand Down
48 changes: 24 additions & 24 deletions fastdds_python/test/api/test_qos.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ def test_datareader_qos():
assert(2 == datareader_qos.resource_limits().extra_samples)

# .user_data
datareader_qos.user_data().push_back(0)
datareader_qos.user_data().push_back(1)
datareader_qos.user_data().push_back(2)
datareader_qos.user_data().push_back(3)
datareader_qos.user_data().append(0)
datareader_qos.user_data().append(1)
datareader_qos.user_data().append(2)
datareader_qos.user_data().append(3)
count = 1
for user_value in datareader_qos.user_data():
if 1 == count:
Expand Down Expand Up @@ -560,10 +560,10 @@ def test_datawriter_qos():
assert(33 == datawriter_qos.lifespan().duration.nanosec)

# .user_data
datawriter_qos.user_data().push_back(0)
datawriter_qos.user_data().push_back(1)
datawriter_qos.user_data().push_back(2)
datawriter_qos.user_data().push_back(3)
datawriter_qos.user_data().append(0)
datawriter_qos.user_data().append(1)
datawriter_qos.user_data().append(2)
datawriter_qos.user_data().append(3)
count = 1
for user_value in datawriter_qos.user_data():
if 1 == count:
Expand Down Expand Up @@ -873,10 +873,10 @@ def test_topic_qos():
topic_qos = fastdds.TopicQos()

# .topic_data
topic_qos.topic_data().push_back(0)
topic_qos.topic_data().push_back(1)
topic_qos.topic_data().push_back(2)
topic_qos.topic_data().push_back(3)
topic_qos.topic_data().append(0)
topic_qos.topic_data().append(1)
topic_qos.topic_data().append(2)
topic_qos.topic_data().append(3)
count = 1
for topic_value in topic_qos.topic_data():
if 1 == count:
Expand Down Expand Up @@ -1091,10 +1091,10 @@ def test_subscriber_qos():
assert('Partition2' == subscriber_qos.partition()[1])

# .group_data
subscriber_qos.group_data().push_back(0)
subscriber_qos.group_data().push_back(1)
subscriber_qos.group_data().push_back(2)
subscriber_qos.group_data().push_back(3)
subscriber_qos.group_data().append(0)
subscriber_qos.group_data().append(1)
subscriber_qos.group_data().append(2)
subscriber_qos.group_data().append(3)
count = 1
for group_value in subscriber_qos.group_data():
if 1 == count:
Expand Down Expand Up @@ -1175,10 +1175,10 @@ def test_publisher_qos():
assert('Partition2' == publisher_qos.partition()[1])

# .group_data
publisher_qos.group_data().push_back(0)
publisher_qos.group_data().push_back(1)
publisher_qos.group_data().push_back(2)
publisher_qos.group_data().push_back(3)
publisher_qos.group_data().append(0)
publisher_qos.group_data().append(1)
publisher_qos.group_data().append(2)
publisher_qos.group_data().append(3)
count = 1
for group_value in publisher_qos.group_data():
if 1 == count:
Expand Down Expand Up @@ -1353,10 +1353,10 @@ def test_domain_participant_qos():
assert(not participant_qos.transport().use_builtin_transports)

# .user_data
participant_qos.user_data().push_back(0)
participant_qos.user_data().push_back(1)
participant_qos.user_data().push_back(2)
participant_qos.user_data().push_back(3)
participant_qos.user_data().append(0)
participant_qos.user_data().append(1)
participant_qos.user_data().append(2)
participant_qos.user_data().append(3)
count = 1
for user_value in participant_qos.user_data():
if 1 == count:
Expand Down
Loading