I got LEAK SUMMARY: definitely lost from valgrind. But as a beginner with valgrind, I do not know to handle this problem.
I think it should be good, when I am using unique pointer.
In the following code I want to
1. calculate how many bytes is needed for my object: DataBlock.
2. aligned_alloc this size (aligned_alloc is for my context, but not relevant here. The valgrind gives me same Leak problem with normal malloc)
3. Then I cast or re interpret this block of the allocated bytes as a Datablock
4. Initial the attributes of datablock (leave out in code)
5. Give back data block as a unique pointer.
Will there be a memory leak problem?
Or it happens to the code, that I left out?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
template <size_t COLUMN_COUNT>
std::unique_ptr<DataBlock<COLUMN_COUNT>> DataBlock<COLUMN_COUNT>::build() {
const uint64_t size_of_data_block = sizeof_db + getDataBytes(table_portion, encodings) + padding;
// some calculation about how many bytes I need to alloc
uint8_t* data_malloc = (uint8_t*)aligned_alloc(32, size_of_data_block);
if (data_malloc == NULL) {
perror("Error allocating memory");
abort();
}
DataBlock<COLUMN_COUNT>* datablock
= reinterpret_cast<DataBlock<COLUMN_COUNT>*>(data_malloc);
// some attributes initialized here
return std::move(std::make_unique<DataBlock<COLUMN_COUNT>>(datablock));
}
| |
Edit: The Datablock is under a template, which has a parameter with the number of the columns.
Some how like that examples:
1 2 3
|
DataBlock<1>* db = new DataBlock(); // 1 column of data
DataBlock<2>* db = new DataBlock(); // 2 columns of data
DataBlock<1>* db = new DataBlock(); // 3 columns of data
| |