FileBrowserComponent and WildcardFileFilter

Hi,

I’m having trouble getting the FileBrowserComponent to work with a file filter. I’m testing under Windows 7, building with VS2005 with the Juce 1.52 SDK.

When I run the app and browse to a folder containing files I get an access violation error in ‘isFileSuitable’, but all the object pointers in the debugger look good except the top of the callStack shows 0x00000000.

This is the method I’m using to instantiate a FileBrowserComponent and add it to the MainComponent:

void MainComponent::createFileBrowser()
{
    File fileFolder = File::getSpecialLocation(File::userHomeDirectory);
    int flags = FileBrowserComponent::openMode | 
        FileBrowserComponent::canSelectFiles |
        FileBrowserComponent::filenameBoxIsReadOnly;
    WildcardFileFilter filter (T("*.mxr"), T("*"), T("Mxr files"));

    // create the browser component
    m_fileBrowser = new FileBrowserComponent(flags,fileFolder, &filter, NULL);

    // add browser compoenent to form and add us as a listener
    addAndMakeVisible(m_fileBrowser);
    m_fileBrowser->setTopLeftPosition(28, 153);
    m_fileBrowser->setSize(307, 298);
    m_fileBrowser->addListener(this);
}

BTW, I also tried subclassing FileFilter and implementing isFileSuitable and isDirectory suitable but that resulted in runtime error “R2065 - pure virtual function call.” The WildcardFileFilter approach seems to be the simpler method, but I’m stuck at this point.

Any clues from the above code on what I’m missing, or pointers to example code that does something similar?

Thanks in advance for any useful info!


Louis Sinclair
aka: rundio

Your FileFilter is on the stack. As soon as that function returns, it is destroyed, so your browser is left with a dangling pointer.
You’ll need a member for the filter too; the browser doesn’t take ownership of the filter, so you need to be in control of its lifetime. The easiest way would be to just have a member instance you initialise in your class’ constructor initialiser list, though of course you could have a pointer and instantiate it manually. Just make sure you don’t forget to delete it! [use a ScopedPointer] :slight_smile:

Duh, I should have seen that, thanks!


Louis

Well, after fixing my silly stack variable error my App hangs on the call to instantiate either the WildcardFileFilter or a subclassed FileFilter. I must be making another silly error. My changed code (using WildcardFileFilter) is:

    void MainComponent::createFileBrowser()
    {
        File fileFolder = File::getSpecialLocation(File::userHomeDirectory);
        int flags = FileBrowserComponent::openMode | 
            FileBrowserComponent::canSelectFiles | FileBrowserComponent::filenameBoxIsReadOnly;
        m_wcFileFilter = new WildcardFileFilter(T("*.mxr"), T("*"), T("Mxr files"));

        // create the browser component
        m_fileBrowser = new FileBrowserComponent(flags,fileFolder, m_wcFileFilter, NULL);

        // add browser compoenent to form and add us as a listener
        addAndMakeVisible(m_fileBrowser);
        m_fileBrowser->setTopLeftPosition(28, 153);
        m_fileBrowser->setSize(307, 298);
        m_fileBrowser->addListener(this);
    }

is that the actual code? because the ‘new FileBrowserComponent’ line is using a variable ‘filter’ which isn’t declared in that function…

Oops, that should be m_wcFileFilter - bad editing.


rundio

I have it working now. Thanks haydxn for looking at it!


Louis
aka:rundio