DirectoryIterator Crash with JUCE_STRING_UTF_TYPE=32


#1

For reasons I can’t disclose, I am required to setup my project with JUCE_STRING_UTF_TYPE=32.

Has anybody encountered crashes when trying to iterate through a directory using a DirectoryIterator, with the flag specified as such? (Only tested on Windows, x86 application)

Here’s a basic example:

DirectoryIterator iterator (File::getSpecialLocation (File::userDesktopDirectory), false);

while (iterator.next()) //Crashes on iteration
    DBG (iterator.getFile().getFullPathName());

#2

I hate to think why you need to set that flag!

Presumably the problem will be somewhere in the String method that returns a UTF-16 string for use in the win32 file functions?


#3

I thought so at first, but after commenting out the NativeIterator instance in the DirectoryIterator, the problem was still there… There seems to be something occurring in the DirectoryIterator’s destructor (dangling pointer deletion, or something).

Believe it or not, doing the following eliminates the issue:

StringArray DirectoryIterator::parseWildcards (const String& pattern)
{
    StringArray s;
    //s.addTokens (pattern, ";,", "\"'");
    s.trim();
    s.removeEmptyStrings();
    return s;
}

Any insight?

Edit: My example in the first post is partly irrelevant; simply creating the DirectoryIterator, and letting it go out of scope demonstrates the crash.


#4

It seems “static CharPointerType StringHolder::createFromCharPointer (const CharPointerType start, const CharPointerType end)” never took into account the CharType. And so, the function wasn’t allocating enough memory, and somewhere else later the relating memory was trying to be deleted out of range.

Fix:

static CharPointerType createFromCharPointer (const CharPointerType start, const CharPointerType end)
{
    if (start.getAddress() == nullptr || start.isEmpty())
        return getEmpty();

    const size_t numBytes = (size_t) (end.getAddress() - start.getAddress());
    const CharPointerType dest (createUninitialisedBytes (numBytes + sizeof (CharType)));
    memcpy (dest.getAddress(), start, numBytes);
    dest.getAddress()[numBytes / sizeof (CharType)] = 0;
    return dest;
}

#5

Ah! Many thanks! I’ll get that sorted out right away!