Big problem with GetOpenFileName().

This is a discussion on Big problem with GetOpenFileName(). within the Windows Programming forums, part of the Platform Specific Boards category; I have had this problem, after I call DoFileDialog() a second time, and I hover over a file, my whole ...

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

    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 11:44 AM.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  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
    1,678
    Okay, fixed. But the problem still remains.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  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,048
    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,048
    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
    1,678
    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.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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 05: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
    1,678
    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.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  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 06: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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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.

Page 1 of 2 12 LastLast
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, 01:54 AM
  2. big endian-small endian problem
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 05-15-2008, 07: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, 10:05 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21