Random crashing with my dictionary class

Programming, for all ages and all languages.
Post Reply
User avatar
scgtrp
Member
Member
Posts: 30
Joined: Sat Mar 28, 2009 7:32 pm

Random crashing with my dictionary class

Post 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.)
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Random crashing with my dictionary class

Post 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
User avatar
scgtrp
Member
Member
Posts: 30
Joined: Sat Mar 28, 2009 7:32 pm

Re: Random crashing with my dictionary class

Post 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()?
User avatar
scgtrp
Member
Member
Posts: 30
Joined: Sat Mar 28, 2009 7:32 pm

Re: Random crashing with my dictionary class

Post 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?
Post Reply