Skip to content

Commit

Permalink
Fix double free issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipeMaia committed Jul 8, 2015
1 parent 724450f commit 30cbbc7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/af/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class AFAPI index {
///
index(const af::array& idx0);

///
/// \brief Copy constructor
///
/// \param[in] idx0 is index to copy.
///
/// \sa indexing
///
index(const index& idx0);

///
/// \brief Returns true if the \ref af::index represents a af::span object
///
Expand All @@ -116,6 +125,17 @@ class AFAPI index {
/// \returns the af_index_t represented by this object
///
const af_index_t& get() const;

///
/// \brief Assigns idx0 to this index
///
/// \param[in] idx0 is the index to be assigned to the /ref af::index
/// \returns the reference to this
///
///
index & operator=(const index& idx0);


};

///
Expand Down
14 changes: 14 additions & 0 deletions src/api/cpp/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,25 @@ index::index(const af::array& idx0) {
impl.isBatch = false;
}

index::index(const af::index& idx0) {
*this = idx0;
}

index::~index() {
if (!impl.isSeq)
af_release_array(impl.idx.arr);
}

index & index::operator=(const index& idx0) {
impl = idx0.get();
if(impl.isSeq == false){
// increment reference count to avoid double free
// when/if idx0 is destroyed
AF_THROW(af_retain_array(&impl.idx.arr, impl.idx.arr));
}
return *this;
}


static bool operator==(const af_seq& lhs, const af_seq& rhs) {
return lhs.begin == rhs.begin && lhs.end == rhs.end && lhs.step == rhs.step;
Expand Down

0 comments on commit 30cbbc7

Please sign in to comment.