This is my first time working with templates in C++ and I was trying to create a simple Queue data structure that can hold any class that I give it. I made a simple test class just to play around with the implementation and whenever I try to free a node I get a heap corruption error. I suspect it has something to do with the size of test class being much larger than the size of the node but I'm not sure why (or if) that is the case. Any help would be greatly appreciated.
QueueTest.h
#include "../../src/Core/Logging/Logging.h"
#include <iostream>
#include <string>
class TestClass {
public:
int num;
std::string str;
TestClass(int inNum, std::string inStr);
};
int TestQueueMain(Logger Logs);
int TestEdgeCase1();
QueueTest.cpp
#include "QueueTest.h"
#include "../../src/Core/DataStructures/Queue.h"
TestClass::TestClass(int inNum, std::string inStr) : num(inNum), str(inStr) {}
std::ostream& operator<< (std::ostream &out, TestClass const& data) {
out << "Num: " << data.num << " Str: " << data.str;
return out;
}
int TestQueueMain(Logger Logs) {
Logs.createLog(Info, "Queue tests started", std::source_location::current());
int failures = 0;
failures += TestEdgeCase1();
Logs.createLog(Info, "Queue tests finished with " + std::to_string(failures) + " error(s)", std::source_location::current());
return failures;
}
int TestEdgeCase1() {
Queue q = Queue<TestClass>();
TestClass t1 = TestClass(1, "a");
std::cout << sizeof(t1) << std::endl; // Prints 48
q.Enqueue(&t1);
std::cout << "test" << std::endl;
TestClass temp = q.Dequeue(); // ERROR HAPPENS HERE
std::cout << temp;
return 0;
}
Queue.h
#include <iostream>
template <class T>
struct QueueNode {
T* data; // Takes a class of whatever data it needs to store
struct QueueNode* next;
};
template <class T>
class Queue {
public:
Queue() : Start(nullptr), End(nullptr), Size(0) {}
~Queue() { Clean(); }
template<class T>
inline void Enqueue(T* Obj) {
QueueNode<T>* node = (QueueNode<T>*)malloc(sizeof(QueueNode<T>*));
std::cout << "Inserting obj: " << *Obj << std::endl;
node->data = Obj;
node->next = nullptr;
if (Start == nullptr) {
Start = node;
End = node;
} else {
End->next = node;
End = node;
}
}
inline T Dequeue() {
if (Start == nullptr) return *Start->data; // Returns nullptr
else if (Start == End) {
std::cout << "START SIZE: " << sizeof(Start) << std::endl; // prints 8
std::cout << "START SIZE2: " << sizeof(Start->data) << std::endl; //prints 8
T val = *Start->data;
End = nullptr;
Start->data = nullptr;
free(Start);
return val;
}
QueueNode<T>* temp = Start;
T val = *Start->data;
Start = Start->next;
free(temp);
return val;
}
inline void Clean() {
struct QueueNode<T>* temp;
while (Start != nullptr) {
temp = Start;
Start = Start->next;
free(temp);
}
free(Start);
std::cout << "Cleaning queue" << std::endl;
}
private:
QueueNode<T>* Start;
QueueNode<T>* End;
int Size;
};
I have tried freeing the node itself, the data portion of the node and setting it to a nullptr (which Im pretty sure would cause a memory leak) but I keep getting this error.