Skip to content

Commit

Permalink
Fix warning about memory leak (#141)
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo González Moreno <[email protected]>
  • Loading branch information
richiware authored Jul 11, 2024
1 parent ffae49c commit 7cbeb21
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 114 deletions.
79 changes: 1 addition & 78 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,14 @@ namespace dds {
}
}

%inline %{
class OctetResourceLimitedVectorStopIterator {};
class OctetResourceLimitedVectorIterator {
public:
OctetResourceLimitedVectorIterator(
eprosima::fastdds::ResourceLimitedVector<eprosima::fastdds::rtps::octet>::iterator _cur,
eprosima::fastdds::ResourceLimitedVector<eprosima::fastdds::rtps::octet>::iterator _end) : cur(_cur), end(_end) {}
OctetResourceLimitedVectorIterator* __iter__()
{
return this;
}
eprosima::fastdds::ResourceLimitedVector<eprosima::fastdds::rtps::octet>::iterator cur;
eprosima::fastdds::ResourceLimitedVector<eprosima::fastdds::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::fastdds::ResourceLimitedVector<eprosima::fastdds::rtps::octet>;
resource_limited_vector_template(OctetResourceLimitedVector, eprosima::fastdds::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::fastdds::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::fastdds::ResourceLimitedVector<eprosima::fastdds::rtps::octet>::__getitem__
{
try
{
$action
}
catch(std::out_of_range)
{
SWIG_exception(SWIG_IndexError, "Index out of bounds");
}
}

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

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

eprosima::fastdds::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 "fastdds/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::fastdds::ResourceLimitedVector::at;
%ignore eprosima::fastdds::ResourceLimitedVector::front;
%ignore eprosima::fastdds::ResourceLimitedVector::back;
%ignore eprosima::fastdds::ResourceLimitedVector::push_back;

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


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


%extend eprosima::fastdds::ResourceLimitedVector {

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

value_type __getitem__(int i)
{
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 "fastdds/utils/collections/ResourceLimitedVector.hpp"

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

name_ ## Iterator* __iter__()
{
return this;
}
typename eprosima::fastdds::ResourceLimitedVector<value_type_>::iterator cur;
typename eprosima::fastdds::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::fastdds::ResourceLimitedVector<value_type_>;

%extend eprosima::fastdds::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.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.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 @@ -76,10 +76,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 @@ -554,10 +554,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 @@ -867,10 +867,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 @@ -1085,10 +1085,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 @@ -1169,10 +1169,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 @@ -1347,10 +1347,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

0 comments on commit 7cbeb21

Please sign in to comment.