Bizarre Bug

Member
Posts: 185
Joined: 2005.02
Post: #1
I have an array of floats that holds object color information. color[3], color[4], and color[5] appear to "die" and revert to zero after a little while. After much searching, I have finally pinpointed the "time of death": a call to alGenSources(). If I try to print the float values before alGenSources, the program prints the values it is suppose to. If I try to print the values after the call to alGenSources, the program prints zeros. I am asking alGenSources to generate 3 sources, if I modify my program and ask it to only generate 2 sources, then color[5] is spared and retains the correct value. What's going on here? Why is alGenSources() "killing" my floats?
Source:
main.c
Code:
#define NUM_Sources 3

ALuint source[NUM_Sources];
Init.c
Code:
extern ALuint source[];
...
void initOpenAL(void)
{
...
/*printf call that shows color array is untouched*/
alGenSources(NUM_Sources, source);
/*printf call that reveals the death of the floats*/
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
You are likely overrunning a buffer, which happens to go into those memory addresses.
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #3
Copy-paste source! Smile
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
It looks like you have NUM_Sources defined in 2 different places. (since you have an extern declaration for source before the function call) Do they match up?
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #5
yeah, both are 3.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
Just out of curiosity, what size do you declare color to be?
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #7
I malloc (or rather calloc) color depending on the size parameter read in from the varibales file. Currently it is an array of 18 floats.
Quote this message in a reply
Member
Posts: 185
Joined: 2005.02
Post: #8
Ok, what was once a minor nuisance has become a program-crashing monstrosity. I finally figures out that sources == &color[3]. It turns out that in main.c I declared color as float color[]; I switched the declaration to float * color = NULL; That fixed it. alGenSources() no longer overwrites color. But now my program flat out crashes! Once it get to the initialization of the main loop it crashes. The debugger is unable to give me the precise point the program crashes at. Here's the source:

main.c
Code:
Stack EngineStateStack;
…
void RunGameCycle(void)
{
…
}

void mainLoop(void)
{
    pushOntoStack(&EngineStateStack, RunGameCycle); /* <-- *** */

*** this is the point that the debugger indicates as the site of the crash, though on the list of threads it is listed a thread #1, #0 has two question marks after it.

Stack.h
Code:
…
typedef void (*vfuncp) ();

typedef struct StackItem
{
    vfuncp funcPoint;
    struct StackItem * lower;
    struct StackItem * higher;
} StackItem;

typedef StackItem * Stack;

…

stack.c

Code:
int pushOntoStack (Stack * pstack, void (*pfunction) (void))
{
    StackItem * pnew;
        
    pnew = (StackItem *) malloc(sizeof(StackItem));
    if (pnew == NULL)
        return 0;
    
    if(*pstack)
    {
        (*pstack)->higher = pnew;
    }
    pnew->funcPoint = pfunction;
    pnew->lower = *pstack;
    pnew->higher = NULL;
    *pstack = pnew;
    
    return 1;
}

If I switch the color declaration back to to float color[] the program does not crash. The debugger tells me the reason for the crash is "EXC_BAD_ACCESS"
Any help?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Bizarre glitch... vnvrymdreglage 8 3,911 Jan 22, 2007 06:14 PM
Last Post: vnvrymdreglage
  Bizarre Conditional Operator Problem in C++ kingofsquirrels 2 2,095 May 23, 2006 02:56 PM
Last Post: kingofsquirrels