Skip to content

Commit

Permalink
Handle self-assignment of some
Browse files Browse the repository at this point in the history
Don't do anything when pointer is self-assigned.
This resolves possible crashes then something
with reference 1 is being self-assigned and deleted
before referencing once again.
  • Loading branch information
lisitsyn committed Jun 12, 2016
1 parent 567ce21 commit be31693
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/shogun/base/some.h
Expand Up @@ -68,9 +68,11 @@ namespace shogun
template <typename T>
Some<T>& Some<T>::operator=(T* other)
{
unref();
raw = other;
ref();
if (raw != other) {
unref();
raw = other;
ref();
}
return *this;
}
template <typename T>
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/base/Some_unittest.cc
Expand Up @@ -39,6 +39,13 @@ TEST(Some,reassignment)
EXPECT_EQ(1, kernel->ref_count());
}

TEST(Some,self_assignment)
{
auto kernel = some<CGaussianKernel>();
kernel = kernel;
EXPECT_EQ(1, kernel->ref_count());
}

TEST(Some,get)
{
auto kernel = some<CGaussianKernel>();
Expand Down

0 comments on commit be31693

Please sign in to comment.