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.)
Random crashing with my dictionary class
Re: Random crashing with my dictionary class
Hi,
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
Code: Select all
Node<T>* previous = current->previous;
Node<T>* next = current->next;
previous->next = next;
next->previous = previous;
Cheers,
James
Re: Random crashing with my dictionary class
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;
}
Re: Random crashing with my dictionary class
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:
to this:
Should this be in the C++ section on the wiki, or am I just stupid for not thinking of that before?
Code: Select all
.text :{
_start_text = .;
*(.text)
_end_text = .;
}
Code: Select all
.text :{
_start_text = .;
*(.text .text.*)
_end_text = .;
}