PDA

View Full Version : Confusing error



Thesuperchang
June 24th, 2009, 02:56 AM
So this method is causing me relentless turmoil! What it's supposed to do is return the value of a colour requested by the user to draw with in the X Window System. If the colour exists, it will locate it in the colour array and return the colour value. If the colour has yet to be invented, the program will make the colour and return the newly made colour.

At the moment it seg. faults whenever a new colour is drawn with. It also seems to have a hard time locating the colours that already exist. I was thinking that it might be a simple bug which I may have overlooked so that is why I'm posting it here.


colour_t& Colour(uint8_t red, uint8_t green, uint8_t blue) {
int index;
colour_t *temp;

if(colourCount == 0) {
// Invent a new colour
colourCount = 1;
multiColour = (colour_t *)malloc(sizeof(colour_t) * colourCount );
if(multiColour == NULL) {
// Freak out!
}
index = colourCount;
RequestColour(red, green, blue, (multiColour[index]));
return multiColour[index];
}
else {
// Look for a match
for(index = 0; index < (colourCount+0); index++) {
if( (multiColour[index].colourID.red == red) &&
(multiColour[index].colourID.green == green) &&
(multiColour[index].colourID.blue == blue) ) {
return multiColour[index];
}
}

// No match
// Invent a new colour
colourCount += 1;
temp = multiColour;
multiColour = (colour_t *)realloc( (void *)multiColour, sizeof(colour_t) * (colourCount + 0) );
if(multiColour == NULL) {
multiColour = temp;
}
index = colourCount;
RequestColour(red, green, blue, (multiColour[index - 1]));
return multiColour[index];
}
}

Cheers,
C. Anderson

monraaf
June 24th, 2009, 03:20 AM
At the moment it seg. faults whenever a new colour is drawn with. It also seems to have a hard time locating the colours that already exist. I was thinking that it might be a simple bug which I may have overlooked so that is why I'm posting it here.



if(colourCount == 0) {
// Invent a new colour
colourCount = 1;
multiColour = (colour_t *)malloc(sizeof(colour_t) * colourCount );
if(multiColour == NULL) {
// Freak out!
}
index = colourCount;
RequestColour(red, green, blue, (multiColour[index]));
return multiColour[index];


You are creating 1 color here, but you are returning color nr.2 which does not exist, same at the end. Remember indexes start at 0.

Thesuperchang
June 24th, 2009, 03:31 AM
You are creating 1 color here, but you are returning color nr.2 which does not exist, same at the end. Remember indexes start at 0.

Spot on. The program no longer seg. faults. Thank you very much. Now the next issue is with finding duplicate colours within the array.

Thesuperchang
June 24th, 2009, 03:47 AM
Spot on. The program no longer seg. faults. Thank you very much. Now the next issue is with finding duplicate colours within the array.

Problem resolved. I forgot to assign the values red, green and blue to the values I use to search.

Can+~
June 24th, 2009, 03:48 AM
Next time: use a Debugger