views:

126

answers:

2

Hello, I'm using Visual Studio 2008 (programming in c). I've a weird problem I worte a program that has 2 threads that runs simultaneously, a recording thread (using audio card to record into memory) and a translation thread (using a speech engine to recognize the words). when I run my program in debug mode (aka setting a breakpoint in the code) it runs great, however when I run in debug mode or release mode (outside the visual studio enviroment) it crashes and give me the following exception: "Unhandled exception at 0x7c911129 in LowLevel.exe: 0xC0000005: Access violation reading location 0x014c7245." My stack looks:

LowLevel.exe!__set_flsgetvalue() Line 256 + 0xc bytes C
LowLevel.exe!_isleadbyte_l(int c=4359676, localeinfo_struct * locinfo=0x00000001) Line 57 C++ LowLevel.exe!PlayDateOfExam(int option=1) Line 2240 + 0x7 bytes C++ LowLevel.exe!NSCThread(void * arg=0x00000000) Line 1585 + 0xb bytes C++ kernel32.dll!7c80b729()
winmm.dll!76b5b294()

I uses the following file in my project "nsc.lib" and WinMM.lib" I'm not really familiar with threads I used a sample (which works great) and built on it. I saw a similiar question year on the forum but I didn't really understand the answers since I'm not familiar with with threads. Can someone help me? Thanks

A: 

If a problem only shows up when not running under the debugger then it is likely to be timing related. Have a look in PlayDateOfExam() at the variables being referenced, and determine which of them could also be messed with by another thread.

It's possible that (for example) another thread is messing with the length of an array. Under the debugger the timing is such that you reference that length before the other thread touches it, and everything is fine. Outside the debugger, when that array length is referenced it has already been screwed up, and you end up running over the bounds of the array. It's impossible to tell what the actual problem is, because there isn't enough information, but that is the sort of problem you are looking for.

WillW
A: 

I think it's like you said and the problem and it's PlayDateOfExam, which suprises me since I'm calling this function all over my code. The function code is:

char* PlayDateOfExam(int option)
{

        char* WaveFileName;
        if (option==1)
        {
            int k=atoi(Global_Input.day);
            if (atoi(Global_Input.day)<10)
            {
                int day=atoi(Global_Input.day);
                day=day % 10;
                char* day_s=(char*)malloc(sizeof(char)*2);
                _itoa(day,day_s,10);
                WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Grades Files\\"));
                strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Grades Files\\");
                strcat(WaveFileName,day_s);
                return (WaveFileName);
            }
            else
            {
                WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Grades Files\\"));
                strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Grades Files\\");
                strcat(WaveFileName,Global_Input.day);
                return (WaveFileName);
            }

        }
        else if (option==2)
        {
            int k=atoi(Global_Input.month);
            if (atoi(Global_Input.month)<10)
            {
                int month=atoi(Global_Input.month);
                month=month % 10;
                char* month_s=(char*)malloc(sizeof(char)*2);
                _itoa(month,month_s,10);
                WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Exams Dates Files\\"));
                strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Exams Dates Files\\");
                strcat(WaveFileName,month_s);
                return (WaveFileName);
            }
            else
            {
                WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Exams Dates Files\\"));
                strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Exams Dates Files\\");
                strcat(WaveFileName,Global_Input.month);
                return (WaveFileName);
            }
    }
        else if (option==3)
        {
            WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Exams Dates Files\\"));
            strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Exams Dates Files\\");
            strcat(WaveFileName,"2000+");
            return(WaveFileName);
        }
        else if (option==4)
        {
            int k=atoi(Global_Input.hour);
            if (atoi(Global_Input.hour)<10)
            {
                int hour=atoi(Global_Input.hour);
                hour=hour % 10;
                char* hour_s=(char*)malloc(sizeof(char)*2);
                _itoa(hour,hour_s,10);
                WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Grades Files\\"));
                strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Grades Files\\");
                strcat(WaveFileName,hour_s);
                return (WaveFileName);
            }
            else
            {
                WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Grades Files\\"));
                strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Grades Files\\");
                strcat(WaveFileName,Global_Input.hour);
                return (WaveFileName);
            }
    }
        else if (option==5)
        {
                int k=atoi(Global_Input.minute);
            if (atoi(Global_Input.minute)<10)
            {
                int minute=atoi(Global_Input.minute);
                minute=minute % 10;
                char* minute_s=(char*)malloc(sizeof(char)*2);
                _itoa(minute,minute_s,10);
                WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Grades Files\\"));
                strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Grades Files\\");
                strcat(WaveFileName,minute_s);
                return (WaveFileName);
            }
            else
            {
                WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Grades Files\\"));
                strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Grades Files\\");
                strcat(WaveFileName,Global_Input.minute);
                return (WaveFileName);
            }
        }
        else if (option==6)
        {
            WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Exam Locations Files\\"));
            strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Exam Locations Files\\");
            strcat(WaveFileName,Global_Input.class_location);
            return (WaveFileName);
        }
        else if (option==7)
        {
            WaveFileName=(char*)malloc(sizeof("\\LowLevel_ASR\\Wave Files\\Exam Locations Files\\"));
            strcpy(WaveFileName,"\\LowLevel_ASR\\Wave Files\\Exam Locations Files\\");
            strcat(WaveFileName,Global_Input.class_number);
            return (WaveFileName);
        }
        return ("Error");
    }

The call to the function is: sndPlaySound(PlayDateOfExam(4),SND_SYNC); which is written in a thread that is running. I don't know why in this segment of the code it crashes

etzarfat
You should edit this in to your question, not post it as an answer.
Alex