Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 30th, 2007, 5:02 PM   #1
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
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.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Aug 30th, 2007, 6:11 PM   #2
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
Can you step through the code and figure out exactly which line is causing the program to bomb?
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Aug 30th, 2007, 6:31 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 31st, 2007, 3:49 AM   #4
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 3 rwm is on a distinguished road
Quote:
Originally Posted by DaWei View Post
has little choice but to run off into the weeds and puke on its shoes.
why so graphic ?
rwm is offline   Reply With Quote
Old Aug 31st, 2007, 12:19 PM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,475
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Very funny.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault jazz C 5 Aug 21st, 2006 4:16 AM
Segmentation Fault Cipher C++ 2 Feb 21st, 2006 3:10 PM
Segmentation Fault!!! mesagsxbkr C 20 Oct 22nd, 2005 6:04 AM
segmentation fault caused by malloc to66 C 7 Apr 8th, 2005 2:44 PM
fprintf segmentation fault Michael Edgar C++ 0 Mar 21st, 2005 1:21 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:05 AM.

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