Skip to content

Commit 766dc78

Browse files
committed
Fix memory releasing in vector
Memory (buffer) should be released if capacity is greater than zero (which means that something was allocated), not if size is greater than zero.
1 parent cff33b2 commit 766dc78

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

include/boost/compute/container/vector.hpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ class vector
293293
/// Move-assigns the data from \p other to \c *this.
294294
vector& operator=(vector&& other)
295295
{
296-
if(m_size){
297-
m_allocator.deallocate(m_data, m_size);
296+
if(capacity() > 0){
297+
m_allocator.deallocate(m_data, capacity());
298298
}
299299

300300
m_data = std::move(other.m_data);
@@ -310,8 +310,8 @@ class vector
310310
/// Destroys the vector object.
311311
~vector()
312312
{
313-
if(m_size){
314-
m_allocator.deallocate(m_data, m_size);
313+
if(capacity() > 0){
314+
m_allocator.deallocate(m_data, capacity());
315315
}
316316
}
317317

@@ -401,11 +401,14 @@ class vector
401401
)
402402
);
403403

404-
// copy old values to the new buffer
405-
::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
404+
if(capacity() > 0)
405+
{
406+
// copy old values to the new buffer
407+
::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
406408

407-
// free old memory
408-
m_allocator.deallocate(m_data, m_size);
409+
// free old memory
410+
m_allocator.deallocate(m_data, capacity());
411+
}
409412

410413
// set new data and size
411414
m_data = new_data;
@@ -430,6 +433,10 @@ class vector
430433
/// Returns the capacity of the vector.
431434
size_type capacity() const
432435
{
436+
if(m_data == pointer()) // null pointer check
437+
{
438+
return 0;
439+
}
433440
return m_data.get_buffer().size() / sizeof(T);
434441
}
435442

@@ -447,11 +454,14 @@ class vector
447454
)
448455
);
449456

450-
// copy old values to the new buffer
451-
::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
457+
if(capacity() > 0)
458+
{
459+
// copy old values to the new buffer
460+
::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
452461

453-
// free old memory
454-
m_allocator.deallocate(m_data, m_size);
462+
// free old memory
463+
m_allocator.deallocate(m_data, capacity());
464+
}
455465

456466
// set new data
457467
m_data = new_data;

0 commit comments

Comments
 (0)