Thread: Big problem with GetOpenFileName().

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

    Big problem with GetOpenFileName().

    I have had this problem, after I call DoFileDialog() a second time, and I hover over a file, my whole program terminates with no warning or error message.
    Code:
    int DoFileDialog(HWND hwnd, BOOL bSave)
    {
       char *pszTempPath = new char[MAX_PATH];
       OPENFILENAME ofn;
       ZeroMemory(pszTempPath, MAX_PATH);
       ZeroMemory(szPath, MAX_PATH);
       ZeroMemory(&ofn, sizeof(ofn));
       ofn.lStructSize = sizeof(ofn);
       ofn.hwndOwner = hwnd;
       ofn.lpstrFilter = "Bitmap Files\0*.bmp\0All Files\0*.*\0\0";
       ofn.lpstrFile = pszTempPath;
       ofn.nMaxFile = MAX_PATH;
       ofn.lpstrDefExt = "*.bmp";
       ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
       ofn.FlagsEx = OFN_EX_NOPLACESBAR;
       BOOL bRet;
       if(bSave)   bRet = GetSaveFileName(&ofn);
       else   bRet = GetOpenFileName(&ofn);
       if(bRet)
       return FALSE;
       strcpy(szPath, pszTempPath);
       delete [] pszTempPath;
       return TRUE;
    }
    After searching this board and google I find out that this is a common problem, however, those people always fix their problems by fixing a buffer overflow problem. I'm smart enough to avoid that from the beggining. So please help! Why isn't this working more than once??
    Last edited by Yarin; 08-21-2007 at 10:44 AM.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    delete[] pszTempPath;
    New[] works only with delete[].
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Okay, fixed. But the problem still remains.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
       if(bRet)
       {
          delete [] pszTempPath;
          return FALSE;
       }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int DoFileDialog(HWND hwnd, BOOL bSave)
    {
       char *pszTempPath = new char[MAX_PATH];
       OPENFILENAME ofn;
       ZeroMemory(pszTempPath, MAX_PATH);
       ZeroMemory(szPath, MAX_PATH);
       ZeroMemory(&ofn, sizeof(ofn));
       ofn.lStructSize = sizeof(ofn);
       ofn.hwndOwner = hwnd;
       ofn.lpstrFilter = "Bitmap Files\0*.bmp\0All Files\0*.*\0\0";
       ofn.lpstrFile = pszTempPath;
       ofn.nMaxFile = MAX_PATH;
       ofn.lpstrDefExt = "*.bmp";
       ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
       ofn.FlagsEx = OFN_EX_NOPLACESBAR;
       BOOL bRet;
       if(bSave)   bRet = GetSaveFileName(&ofn);
       else   bRet = GetOpenFileName(&ofn);
       if(bRet)
       return FALSE;
       strcpy(szPath, pszTempPath);
       delete [] pszTempPath;
       return TRUE;
    }
    What if the blue code is true? You'll return a value before delete[]ing pszTempPath. That's probably a memory leak . . . [edit] Beaten again. [/edit]

    Also, if the blue code is true, then szPath is not touched. Is szPath initialized somewhere else? Because if it isn't, and you expect szPath to be initialized after calling this function, you might get a surprise . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by dwks View Post
    Is szPath initialized somewhere else?
    Code:
       ZeroMemory(szPath, MAX_PATH);
    don't see where it's declared though.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Didn't see that. It's probably a global char array. And hopefully contains at least MAX_PATH elements.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Okay, all ready fixed those. Look, I KNOW my char arrays arn't the problem, I've had this with many of my programs before.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Yarin View Post
    After searching this board and google I find out that this is a common problem, however, those people always fix their problems by fixing a buffer overflow problem. I'm smart enough to avoid that from the beggining. So please help! Why isn't this working more than once??
    Quote Originally Posted by Yarin View Post
    Okay, all ready fixed those. Look, I KNOW my char arrays arn't the problem, I've had this with many of my programs before.
    Bwahahahahahahaha.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Make sure the lpstrFile member of the OPENFILENAME struct is null-terminated.

    While not specific to your problem, if you already know the required size of your 'pszTempPath' variable is MAX_PATH then there's no need to use dynamic memory allocation.

    ie.
    Code:
    char pszTempPath[MAX_PATH]="\0";
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    just a note:

    char pszTempPath[MAX_PATH]="\0";
    is absolutely necessary for GetOpenFileName to function properly. from experience I know that both GetOpenFileName and GetSaveFileName seem to test the string, and will fail if it is not initialized to null. it doesn't apply to your problem however, as I see you are zeroing the pszTempPath memory block.

    but shouldn't this:

    Code:
    if(bRet)
    {
       delete [] pszTempPath;
       return FALSE;
    }
    be:

    Code:
    if(!bRet)
    {
       delete [] pszTempPath;
       return FALSE;
    }
    Last edited by Bleech; 08-21-2007 at 04:50 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  12. #12
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Yes, I fixed that in the original code.

    Here, I'll just down what I have now
    Code:
    char szPath[MAX_PATH];
    
    ...
    
    int DoFileDialog(HWND hwnd, BOOL bSave)
    {
       OPENFILENAME ofn;
       ZeroMemory(szPath, MAX_PATH);
       ZeroMemory(&ofn, sizeof(ofn));
       ofn.lStructSize = sizeof(ofn);
       ofn.hwndOwner = hwnd;
       ofn.lpstrFilter = "Bitmap Files\0*.bmp\0All Files\0*.*\0\0";
       ofn.lpstrFile = szPath;
       ofn.nMaxFile = MAX_PATH;
       ofn.lpstrDefExt = "*.bmp";
       ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
       ofn.FlagsEx = OFN_EX_NOPLACESBAR;
       BOOL bRet;
       if(bSave)   bRet = GetSaveFileName(&ofn);
       else   bRet = GetOpenFileName(&ofn);
       if(!bRet)   return FALSE;
       return TRUE;
    }
    as you can see allocations are no longer the problem here (I think).
    Like I said up top, it works the first time, and will only crash the 2nd+ time if I hover over somthing long enough for the little yellow details box to come up.

  13. #13
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    there are only 2 other things that I can think of.

    1. it may be your ofn.Flags field. if you're opening a file, OFN_OVERWRITEPROMPT doesn't really make sense. I'd say try something like:

    Code:
    if(bSave)
       ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
    else
       ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST;
    2. the little yellow details box you're talking about might have something to do with the OFN_EXPLORER flag (Explorer has the same feature, if you're talking about hovering the cursor over a file to view brief details). note that I added it in the above example, it doesn't hurt to give it a try.

    otherwise I don't know what it could be.
    Last edited by Bleech; 08-21-2007 at 05:57 PM. Reason: grammar mistakes

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Have you tried this code in a minimal program, which essentially exists only to call this function, and nothing else?

    Does it work as expected ?

    If so, then there is no problem with this particular function, and the problem is really elsewhere in your code.

    Remember, if you're trashing memory (despite your protests), the effect of "not working" is often far removed from the true cause of the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Another thing to consider is that Get[Save/Open]FileName are re-entrant. Your window procedure(s) will continue to be called while waiting for them to return. Consider what will happen if you are using static or global variables in your window procedure(s) and check WM_PAINT carefully, as it is likely to be called when the details tool tip disappears.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. big number problem
    By mad_muppet in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 12:54 AM
  2. big endian-small endian problem
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 05-15-2008, 06:47 PM
  3. Plzzzzz Help Me >>> Big Problem
    By AHMED KHALAF in forum C Programming
    Replies: 2
    Last Post: 12-09-2004, 07:31 AM
  4. [Help] C++ Problem
    By Oxide in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2004, 09:05 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM