Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Segmentation Fault! (http://www.programmingforums.org/showthread.php?t=13875)

Prm753 Aug 30th, 2007 4:02 PM

Segmentation Fault!
 
I keep getting a Segmentation Fault in my program and I cannot figure it out. I don't have much experience with that sort of thing, so maybe you could give me some advice.

Here is the code that seems to be generating the segfault:

:

BOOL PathExists(const char* pathtocheck)
{
    struct stat St;
    BOOL bRes = stat( pathtocheck, &St );
}

int IsInstalled(char *envPath, char *folderPath, int pathCheck)
{
    char tempEnv[1024];
    GetEnvironmentVariable(envPath, tempEnv, MAX_PATH);
    strcat(tempEnv, folderPath);
    // strcat(envPath, tempEnv);
    // strcpy(fullPath, tempEnv);
   
    if ( PathExists(tempEnv) == 0 )
      pathCheck = 1;
     
    // free(tempEnv);
    // free(envPath);
   
    // return pathCheck;
   
}


I'm calling the functions here:

:

case ID_IS_INSTALLED:
                {
                    SendDlgItemMessage(hDlgWnd, IDC_DLG_TEXT, EM_REPLACESEL, FALSE, 0);
                   
                    char buffer[MAX_PATH];
                    char fullPath[MAX_PATH];
                    char tempBuffer[1024];
                    int check, checkTwo, checkThree, checkFour,
                    checkFive, checkSix, checkSeven, checkEight,
                    checkNine, checkTen, checkEleven;
                    // HWND hwnd = GetDlgItem(hDlgWnd, IDC_DLG_TEXT);
                   
                    check = IsInstalled("PROGRAMFILES", "\\Viewpoint\\Viewpoint Media Player", check);
                    checkTwo = IsInstalled("PROGRAMFILES", "\\Viewpoint\\Viewpoint Manager", checkTwo);
                    checkThree = IsInstalled("ALLUSERSPROFILE", "\\Viewpoint\\Viewpoint Manager", checkThree);
                    checkFour = IsInstalled("ALLUSERSPROFILE", "\\Viewpoint\\Viewpoint Media Player", checkFour);
                    checkFive = IsInstalled("PROGRAMFILES", "\\Viewpoint\\Common", checkFive);
                    checkSix = IsInstalled("PROGRAMFILES", "\\Viewpoint\\Viewpoint Experience Technology", checkSix);
                    checkSeven = IsInstalled("PROGRAMFILES", "\\Viewpoint\\Viewpoint Toolbar V35", checkSeven);
                    checkEight = IsInstalled("PROGRAMFILES", "\\Viewpoint\\Viewpoint Toolbar", checkEight);
                    checkNine = IsInstalled("ALLUSERSPROFILE", "\\Viewpoint\\Viewpoint Toolbar", checkNine);
                    checkTen = IsInstalled("ALLUSERSPROFILE", "\\Viewpoint\\Toolbar Runtime", checkTen);
                    checkEleven = IsInstalled("CommonProgramFiles", "\\Viewpoint\\Toolbar Runtime", checkEleven);
                   
                    strcpy(buffer, "");
                    strcat(buffer, "Viewpoint Media Player?\t");
                    if (check == 1 || checkFour == 1  || checkFive == 1 || checkSix == 1)
                        strcat(buffer, "YES");
                    else
                        strcat(buffer, "No");

                    strcat(buffer, "\r\n");
                    strcat(buffer, "Viewpoint Manager?\t");
                    if (checkTwo == 1 || checkThree == 1)
                        strcat(buffer, "YES");
                    else
                        strcat(buffer, "No");

                    strcat(buffer, "\r\n");
                    strcat(buffer, "Viewpoint Toolbar?\t\t");
                    if (checkSeven == 1 || checkEight == 1 || checkNine == 1 || checkTen == 1 || checkEleven == 1)
                      strcat(buffer, "YES");
                    else
                      strcat(buffer, "No");
                   
                    // SetDlgItemText(hDlgWnd, IDC_DLG_TEXT, buffer);
                    // SetWindowText(hwnd, buffer);
                    SetDlgItemText(hDlgWnd, IDC_DLG_TEXT, buffer);
                   
                    // free(buffer);
                    free(tempBuffer);
                    free(fullPath);
                   
                }
                break;


I commented out some of those functions trying to figure out the problem. I really have no idea what to do. Some advice would be greatly appreciated. Thank you. :)

Kilo Aug 30th, 2007 5:11 PM

Can you step through the code and figure out exactly which line is causing the program to bomb?

DaWei Aug 30th, 2007 5:31 PM

You cannot free memory which you have not gotten from the heap. Local variables such as tempBuffer are autovariables. These are allocated (in most implementations, on the stack) when the function is entered. They are deallocated when the function returns.

When you use free, you are telling the system to free heap memory located at the pointer that you received when the memory was allocated. Since the auto variable was not allocated from the heap, the heap has no record regarding it, and has little choice but to run off into the weeds and puke on its shoes.

rwm Aug 31st, 2007 2:49 AM

Quote:

Originally Posted by DaWei (Post 133029)
has little choice but to run off into the weeds and puke on its shoes.

why so graphic ? :D

Infinite Recursion Aug 31st, 2007 11:19 AM

Very funny. :)


All times are GMT -5. The time now is 10:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC