Page 1 of 1
Random crashing with my dictionary class
Posted: Sat Mar 28, 2009 7:46 pm
by scgtrp
I've written this dictionary template for my kernel. I tested it in Linux, and it worked fine, but when I try it in my kernel, I get seemingly random errors: sometimes the screen fills with random colors, sometimes I get a general protection fault, sometimes it just hangs. Am I doing anything obviously not suitable for kernel code here?
http://pastebin.ca/1375326 -- the dictionary class
http://pastebin.ca/1375332 -- the linked list class, which it uses - gdb says the crash is in LinkedList::count(), although I see nothing which could possibly crash there. (Yes, *this is valid.)
Re: Random crashing with my dictionary class
Posted: Sat Mar 28, 2009 8:01 pm
by JamesM
Hi,
Code: Select all
Node<T>* previous = current->previous;
Node<T>* next = current->next;
previous->next = next;
next->previous = previous;
You're not checking if 'previous' or 'next' are NULL, which will happen at the end of or start of the list. You're thus dereferencing a null pointer and writing, which will cause you bother.
Cheers,
James
Re: Random crashing with my dictionary class
Posted: Sun Mar 29, 2009 8:34 am
by scgtrp
Code: Select all
Node<T>* newnode = new Node<T>(data);
if (current == NULL)
{
// add as only node
current = newnode;
newnode->next = newnode;
newnode->previous = newnode;
head = current;
}
else
{
// add at end
Node<T>* tail = head->previous;
head->previous = newnode;
newnode->next = head;
tail->next = newnode;
newnode->previous = tail;
if (go_to)
current = newnode;
}
This is a circular linked list, so there is no 'start' or 'end' of the list - it just wraps around. current == NULL iff there are no items. And why is it crashing in count()?
Re: Random crashing with my dictionary class
Posted: Sun Mar 29, 2009 5:15 pm
by scgtrp
Got it. Apparently GCC puts template stuff in its own section in .o files called things like .text._ZN10LinkedListIN10DictionaryImhLh0EE4ItemEE3addEPS2_b. My kernel attempts to be intelligent about where it can start allocating RAM, and completely ignored these sections since they weren't in the real .text, and started happily allocating out chunks of the template code for use as linked list nodes. The solution was to change this in my linker script:
Code: Select all
.text :{
_start_text = .;
*(.text)
_end_text = .;
}
to this:
Code: Select all
.text :{
_start_text = .;
*(.text .text.*)
_end_text = .;
}
Should this be in the C++ section on the wiki, or am I just stupid for not thinking of that before?