Skip to content

Commit db27aaf

Browse files
committed
added destructor on push back to handle memmory leaks ogf objects
1 parent ebe620a commit db27aaf

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ target_include_directories(${PROJECT_NAME} INTERFACE ${${PROJECT_NAME}_SOURCE_DI
1414

1515
add_subdirectory(tests)
1616

17+
add_subdirectory(samples)

circular_buffer.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,14 @@ inline
210210
void CircularBuffer<T>::clear(){
211211
std::lock_guard<std::mutex> _lck(_mtx);
212212
_head = _tail = _size = _max_size = 0;
213-
//_full = false;
214213
}
215214

216215
template<typename T>
217216
inline
218217
typename CircularBuffer<T>::size_type CircularBuffer<T>::size() const{
219218
std::lock_guard<std::mutex> _lck(_mtx);
220219
return _size;
221-
/*(if(_full)
222-
return _max_size;
223-
else{
224-
if(_head >= _tail)
225-
return _head -_tail;
226-
else
227-
return _max_size - _tail +_head;
228-
}*/
229-
}
220+
}
230221

231222
template<typename T>
232223
inline
@@ -267,6 +258,8 @@ typename CircularBuffer<T>::const_reference CircularBuffer<T>::back() const{
267258
template<typename T>
268259
void CircularBuffer<T>::push_back(const T& data){
269260
std::lock_guard<std::mutex> _lck(_mtx);
261+
if(full())
262+
_buff[_head].~T();
270263
_buff[_head] = data;
271264
_increment_bufferstate();
272265
}

samples/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
set(CMAKE_CXX_STANDARD 11)
2+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3+
4+
add_executable(sample_circularbuffer sample_circularbuffer.cpp)
5+
target_link_libraries(sample_circularbuffer
6+
PRIVATE
7+
circularbuffer
8+
)
Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,32 @@
66
#include "circular_buffer.h"
77
#include <iostream>
88
#include <string.h>
9-
#include "gtest/gtest.h"
109

1110
struct test_struct{
12-
int i;
1311
static int count ;
1412
char* bytes =nullptr;
1513
test_struct(){
16-
i=0;
17-
bytes = (char*)malloc(10000000);
14+
bytes = (char*)malloc(100);
1815
std::cout<<"constructing test_struct: "<<count++<<"\n";
1916
}
17+
test_struct(const test_struct& other){
18+
bytes = (char*)malloc(100);
19+
memcpy(bytes, other.bytes,100);
20+
std::cout<<"copy constructor called for test_struct \n";
21+
}
22+
23+
test_struct& operator=(const test_struct& other){
24+
bytes = (char*)malloc(100);
25+
memcpy(bytes, other.bytes,100);
26+
std::cout<<"assignment operator called for test_struct \n";
27+
return *this;
28+
}
29+
2030
~test_struct(){
21-
std::cout<<"destructing test_struct\n";
31+
std::cout<<"destructing test_struct"<<--count<<"\n";
2232
free(bytes);
2333
}
34+
2435

2536
};
2637

@@ -78,6 +89,22 @@ int main(int argc, char *argv[])
7889
std::cout<<"Checking deference * operator "<<*it<<"\n";
7990
std::cout<<"Checking deference ++ operator "<<*(++it)<<"\n";
8091
std::cout<<"Checking deference -- operator "<<*(--it)<<"\n";
92+
93+
CircularBuffer<test_struct> test_structbuf{5};
94+
auto temp_struct = test_struct();
95+
test_structbuf.push_back(temp_struct);
96+
test_structbuf.push_back(temp_struct);
97+
test_structbuf.push_back(temp_struct);
98+
test_structbuf.push_back(temp_struct);
99+
test_structbuf.push_back(temp_struct);
100+
test_structbuf.push_back(temp_struct);
101+
test_structbuf.push_back(temp_struct);
102+
test_structbuf.push_back(temp_struct);
103+
test_structbuf.push_back(temp_struct);
104+
test_structbuf.push_back(temp_struct);
105+
106+
107+
test_structbuf.pop_front();
81108

82109

83110
return 0;

0 commit comments

Comments
 (0)