I'm trying to create a linked list class template (Yes, I know there's one in the c++ library but I wanted to create my own for fun). I've traced through the code and all seems well until the program exits.
Here's the used code:
list.h:
#ifndef LIST_H
#define LIST_H
#include "misc.h"
template <typename T> class CList {
private:
class CNode {
friend CList;
private: T data;
CNode* next;
public: CNode() : next(NULL) {}
~CNode() { delete [] next; }
};
private: int length;
CNode* first;
public:
CList() : length(0), first(NULL) {}
CList(int i_length) : first(NULL) {
int i;
CNode* cur = NULL;
CNode* prev = NULL;
if (i_length < 0) length = 0;
else length = i_length;
for (i=0;i<length;i++) {
// allocate new CNode on heap
cur = new2<CNode>();
// attach preceding CNode pointer
if (prev) prev->next = cur;
else first = cur;
prev = cur;
}
}
~CList() { delete first; }
};
misc.h
#ifndef MISC_H
#define MISC_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
inline void terminate( const char* message, int code ) {
printf("\n\n%s\n\n",message);
system("pause");
exit(code);
};
template <typename T> inline T* new2() {
T* ret = new T;
if (!ret) terminate("Insufficient Memory",-2);
return ret;
}
template <typename T> inline T* new2(int num) {
if (num <= 0) terminate("Invalid Argument",-1);
T* ret = new T[num];
if(!ret) terminate("Insufficient Memory",-2);
return ret;
}
#endif
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "../Misc/misc.h"
#include "../Misc/list.h"
int main(int argc, char* argv[]) {
//CList<int> m;
CList<int> n(5);
system("pause");
return 0;
}
Here is what the variable "n" looks like at the breakpoint just before "return 0;".
http://s20.beta.photobucket.com/user/marshallbs/media/Untitled_zps52497d5d.png.html
Here's the context in which the error occurs. Unfortunately at this point I can no longer view the variable "n" on the watch list.
_mlock(_HEAP_LOCK); /* block other threads */
__TRY
/* get a pointer to memory block header */
pHead = pHdr(pUserData);
/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
There is no error when I use the default constructor for my list. I don't understand what's going on as the memory release process should stop when it reaches the fifth CNode object which has a null "next" pointer. It acts as though it's trying to releasing an invalid non-null pointer but I don't see how this can happen.
One problem is that you allocate next
using new
and free it using delete[]
. This is undefined behaviour.
Allocation:
cur = new2<CNode>(); // new2 uses `new' and not `new[]'
Deallocation:
~CNode() { delete [] next; }
Replace the latter with delete next;
.
I built and ran (from the debugger) the code as-is and got no assertion failures. In fact, there is no memory deallocation at all because CList doesn't have a destructor (didn't you post the complete code?).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.