So, I know there are a million threads out there, and it's almost always to do with pointers. I'm at a loss though, I've debugged the code step by step, and everything looks fine to me. Which means I am misunderstanding something.
The program is a dashboard... collects and displays a bunch of information. Since both the amount of information can be different (ie sometimes no information to display) and the information itself can be very different, I elected to use a singly linked list to store the information. Each node contains the string and an X,Y coord. The list itself contains the font and color of the text.
Dummyclass was just written on-the-spot to save you from reading a bunch of extra code... in my actual application, it's an abstract class. The Update function is a pure virtual, which is called via a wrapper in the base class to allow the use of _beginthread(). The following member functions are my multi-threaded implementation.
1 2 3 4 5 6 7
|
void ThreadUpdate(){
_beginthread(&ParentClass::UpdateWrapper,0,static_cast<void*>(this));
}
static void __cdecl UpdateWrapper(void* o){
static_cast<ParentClass*>(o)->UpdateFunction();
}
|
I have comments in the code indicating the line that fails. It is within the TextArray destructor.
When stopped at the breakpoint, the char* has a valid null-terminated value in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
//////////////////////My node
struct TextArray{
char* text;
int X,Y;
TextArray *next;
TextArray(char* text,int x, int y){
this->next=NULL;
this->text=(char*)calloc(strlen(text),sizeof(char));
strcpy(this->text,text);
X=x;
Y=y;
}
~TextArray(){
if (text!=NULL)
free(text); ///////////////////THIS LINE FAILS
this->next=NULL;
}
};
///////////////////////The list itself
struct TextArrayList{
TextArray *head;
int Nodes;
HFONT GlobalFont;
COLORREF GlobalColor;
TextArrayList(HFONT RefToGlobalFont,COLORREF RefToGlobalColor){
GlobalFont=RefToGlobalFont;
GlobalColor=RefToGlobalColor;
Nodes=0;
head=NULL;
}
TextArrayList(HFONT RefToGlobalFont){
GlobalFont=RefToGlobalFont;
GlobalColor=black;
Nodes=0;
head=NULL;
};
~TextArrayList(){
TextArray *temp;
while (head!=NULL){
temp=head;
head=head->next;
delete temp;
};
if (head!=NULL)
delete head;
}
int AddToList(TextArray *NewNode){
if (Nodes==0)
head=NewNode;
else{
TextArray *temp=head;
while (temp->next!=NULL)
temp=temp->next;
temp->next=NewNode;
}
Nodes++;
return Nodes;
}
void Clear(){
while (head!=NULL){
TextArray *temp;
temp=head;
head=head->next;
delete temp;
}
this->head=NULL;
Nodes=0;
}
};
/////////////The implementation
class dummyclass {
public:
dummyclass(){list=(TextArrayList*)calloc(1,sizeof(TextArrayList));};
~dummyclass(){free(list);};
void UpdateFunction(){list->Clear();doSomething();}; ////this calls Clear()
private:
TextArrayList *list;
};
|
I have no idea why I get a heap corruption error when I try to free the char*... it was calloc'd, so I would assume it would need to be free()ed?