getOpenFileName image find

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

getOpenFileName image find

Post by LunaRebirth »

Hey guys,
I'm using getOpenFileName to have someone open the directory window and choose a file.
When I use getOpenFileName and the window pops up, if I close the window or cancel, it'll close normally.
But when I press Open on a file I've selected in the window, all of my Images disappear and out prints a bunch of "Could not open file of texture: filename.png" (even though it's the correct image path, just disappears).

I don't understand why this is happening.

Code: Select all

char* openFName() {
        std::string fileNameStr="";
 
        #ifdef _WIN32 || _WIN64
        char *filter = "All Files (*.*)\0*.*";
        HWND owner = NULL;
        OPENFILENAME ofn  ;
        char fileName[MAX_PATH] = "";
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(OPENFILENAME);
        ofn.hwndOwner = owner;
        ofn.lpstrFilter = filter;
        ofn.lpstrFile = fileName;
        ofn.nMaxFile = MAX_PATH;
        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
        ofn.lpstrDefExt = "";
        ofn.lpstrInitialDir ="";
 
        if ( GetOpenFileName(&ofn) ) {
            fileNameStr = fileName;
        }
        //Now we're left with either fileNameStr = "" or fileNameStr = "/correctFilePath"
        return (char*)fileNameStr.c_str();
}
Any help is appreciated :-)
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: getOpenFileName image find

Post by hendu »

Your returned pointer is invalid. The object disappears when the function returns.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: getOpenFileName image find

Post by LunaRebirth »

I don't understand how the returned pointer is invalid
MartinVee
Posts: 139
Joined: Tue Aug 02, 2016 3:38 pm
Location: Québec, Canada

Re: getOpenFileName image find

Post by MartinVee »

The c_str() function returns a pointer to the underlying data in your std::string. Since fileNameStr is local to the function, as soon as you return from the function, it gets deleted because it falls out of scope, and thus, the pointer returned by c_str() is no longer valid.

By the way, you shouldn't cast out the const qualifier, as the data pointed by a std::string should never be modified directly.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: getOpenFileName image find

Post by LunaRebirth »

Okay so when I avoid the std::string and c_str() by doing

Code: Select all

const char* openFName() {
        const char* fileNameStr="";
 
        char *filter = "All Files (*.*)\0*.*";
        HWND owner = NULL;
        OPENFILENAME ofn  ;
        char fileName[MAX_PATH] = "";
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(OPENFILENAME);
        ofn.hwndOwner = owner;
        ofn.lpstrFilter = filter;
        ofn.lpstrFile = fileName;
        ofn.nMaxFile = MAX_PATH;
        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
        ofn.lpstrDefExt = "";
        ofn.lpstrInitialDir ="";
 
        if ( GetOpenFileName(&ofn) ) {
            fileNameStr = fileName;
        }
        //Now we're left with either fileNameStr = "" or fileNameStr = "/correctFilePath"
        return fileNameStr;
}
I'm left with the same issue.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: getOpenFileName image find

Post by LunaRebirth »

Actually, if I return "", and completely remove fileNameStr, it still removes all images from my game.
I'm assuming the issue is something with my ofn
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: getOpenFileName image find

Post by LunaRebirth »

I've found that after selecting a file in OpenFileName, it's as if my program loses rights to opening any file.
If I read a file before OpenFileName select, the read is successful. After OpenFileName select, it says the same file couldn't be read
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: getOpenFileName image find

Post by hendu »

You still used a local variable.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: getOpenFileName image find

Post by LunaRebirth »

Code: Select all

const char* fileNameStr="";
char *filter = "All Files (*.*)\0*.*\0\0";
HWND owner = 0;
OPENFILENAME ofn;
char fileName[MAX_PATH] = "";
const char* Game::getOpenFileName() {
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = owner;
    ofn.lpstrFilter = filter;
    ofn.lpstrFile = fileName;
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "";
    ofn.lpstrInitialDir ="";
 
    GetOpenFileName(&ofn);
 
    return fileNameStr;
}
Same issue. Sorry if I'm not understanding
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: getOpenFileName image find

Post by CuteAlien »

Now you return a global variable which is only initialized with "" but never changed.

The problem above (previously posted code, not the one directly above this post) is - you set your pointer to a string (called filename) which is only on the stack. That memory is invalid as soon as your function returns. So the pointer (fileNameStr) is now basically pointing to whatever is now written in memory (which can be anything - sometimes you get lucky and the old content is still there, otherwise you might get anything else). Be lazy instead and work with something like std::string or irr::stringc ... those classes care about memory handling internally so you can return them from a local function (and with modern c++11 compilers that's even done very effective internally). You can't do that with pointers when the stuff they point to is only memory on the stack. With pointers you have to allocate that memory yourself and then copy the string to it. And ensure that memory stays valid. For example by passing the already allocated memory to the function (and yes that is tricky because you don't know which size it will need - so you either allocate so much outside that it will be guaranteed enough or you have to call a function first which tells you how much memory will be needed and then allocated that). Alternatively (but this is mostly bad design) by allocating it inside the function with new and then returning it (and you have to make sure to clean it up then somewhere or you get a memory leak). Reason that's bad design is that it's usually easier if the allocation and destruction is happening on the same level in your code (so if a function allocates it but can't release it because it has to return the memory you will have to start documenting this and make people who use the function aware that they have to clean up some memory later on).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: getOpenFileName image find

Post by LunaRebirth »

I guess what I'm not understanding is how this is removing my images.
Even though my function returns "" because it's never changed, it still removes all 2D images and says they couldn't be found.
I'm still messing with the code to see what works, but as of the moment, nothing is solving the problem.
Any time GetOpenFileName is called and I select an existing file on the window, that's when the issues occur. If I select nothing and either press the X or cancel, I'm returned with "" and there are no issues -- everything is good.

I'll try working with std::string or irr::stringc to see what works, but the problem is persisting at the moment
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: getOpenFileName image find

Post by Seven »

it still removes all 2D images and says they couldn't be found.

is the gui dialog changing the working directory?
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: getOpenFileName image find

Post by LunaRebirth »

I'm unsure on the working directory.
Nothing leads me to believe that's the issue, though it sounds accurate from the errors I'm receiving.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: getOpenFileName image find

Post by Seven »

Found on net



When the user selects at least one file and clicks the OK button, the process' current working directory is changed to the directory contain the file(s) being opened.
LunaRebirth
Posts: 386
Joined: Sun May 11, 2014 12:13 am

Re: getOpenFileName image find

Post by LunaRebirth »

Ohhh I see.
Hmm, I searched the whole internet for that quote you found and it was pretty well hidden into the community editions of https://technet.microsoft.com/sr-latn-r ... y/ms646927
Thanks :)
Post Reply