Probleme with FileBrowserComponent


#1

Hello everyone,

I get a strange problem with FileBrowserComponent and i can't figure out why.

This is my piece of code :

///get the desktop file of the user
File desktopFile = File:: getSpecialLocation(File::userDesktopDirectory);

//create a ScopedPointer for the audio file filter
wildcardFilter = new WildcardFileFilter(String("*.wav,*.aif,*.aiff,*.mp3"), "*", String("Audio files"));

//create the flags of the FileBrowserComponent
 int flags = FileBrowserComponent::openMode |
        FileBrowserComponent::canSelectFiles |
        FileBrowserComponent::filenameBoxIsReadOnly;

//create a ScopedPointer<FileBrowserComponent> with the filter and desktop as default directory
browser =new FileBrowserComponent(flags ,
desktopFile,
wildcardFilter,
nullptr);

//Create a FileChooserDialogBox with my FileBrowserComponent
FileChooserDialogBox dialogBox (String("Choose an audio file"),
String("Please choose some audio files that you want to open..."),
*browser,
false,
Colours::lightgrey);

////Use the selected files
if (dialogBox.show())
{
int numberOfFiles = browser->getNumSelectedFiles ();
StringArray mesfiles;
for(int i=0;i<numberOfFiles;++i)
{
File selectedFile = browser->getSelectedFile (i);
mesfiles.add(selectedFile.getFullPathName ());
DBG(selectedFile.getFullPathName ());
}}

This piece of code work well when i use it alone in a dedicated application for it.

When I add it in a function of my program it work well when i compile in release mode but  when i compile in debug mode, MS Visual studio lunch an exception with the following text :

"Unhandled 0x00452AA0 in myprogram.exe Exception: 0xC0000005: Access Violation when writing to the location 0x00A28B74."

and stop at this line of the juce_String.cpp code:

static CharPointerType makeUniqueWithByteSize (const CharPointerType text, size_t numBytes)
    {
        StringHolder* const b = bufferFromText (text);

        if (b == (StringHolder*) &emptyString)
        {
            CharPointerType newText (createUninitialisedBytes (numBytes));
            newText.writeNull();
            return newText;
        }

///line where the program stop
        if (b->allocatedNumBytes >= numBytes && b->refCount.get() <= 0)
            return text;

        CharPointerType newText (createUninitialisedBytes (jmax (b->allocatedNumBytes, numBytes)));
        memcpy (newText.getAddress(), text.getAddress(), b->allocatedNumBytes);
        release (b);

        return newText;
    }

Do someone know why it could have this behavior and don't work only in debug mode? What could cause the problem?

I'm sorry to ask a so stupid question on this forum but i'm a newbie with Juce and C++ programing. It is not my regular work. Maybe someone can have an answer for me.

Thanks.

 


#2

Other interesting things, when i compile in x64 on my windows 7 64bits in debug mode it works well. The Crash append only if i compile in  the win32 mode in debug mode.

Someone know why?


#3

Just because you only see the crash in debug mode doesn't mean your other builds are correct.

No way to magically tell you what's wrong with your code, but it's a basic C++ dangling pointer bug - you're using an object after deleting it.