0

On x86, it may fail to initialize QImage on worker thread. (Rare in x64)

The probability increases when parallel processing is performed over the number of cores of the CPU.

This occurs not only by reading from an image file, but also by initializing a plain QImage by specifying its size, or simply by calling QImage::copy().

This is a code to avoid it. Of course it is not perfect. Please tell me a better way.

QImage createImageAsync(QString path)
{
    QImageReader reader(path);
    if(!reader.canRead())
       return QImage();
    // QImage processing sometimes fails
    QImage src;
    int count = 0;
    do {
        src = reader.read();
        if(!src.isNull())
            break;
        if(src.isNull() && count++ < 1000) {
            QThread::currentThread()->usleep(1000);
            continue;
        }
        return QImage();
    } while(1);
    return src;
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Hae you tried to find out how much memory your program uses? On 32 bit windows systems each process have a 2GB limit. Also, are you sure it's malloc that fail? if yes how did you found out. Do you have any error messages? – litelite Aug 04 '17 at 13:48
  • The output "QImage: out of memory, returning null image" appears. Examining the implementation of QImage, I found that the root cause is due to failure of malloc. Memory usage is about 600 MB (private working set). – Kanryu KATO Aug 04 '17 at 13:55
  • Does your computer have plenty of memory available? And how did you measure the used memory? – litelite Aug 04 '17 at 14:09
  • My PC is on Windows 7 x64 and have 16GB memory. – Kanryu KATO Aug 04 '17 at 14:11

1 Answers1

0

Essentially this problem was caused by heap memory fragmentation.

Therefore, as a solution, it is conceivable to replace the memory allocator with tcmalloc or jemalloc, for example.

In my application, it was confirmed that this problem can be sufficiently avoided by limiting the number of image files that are opened at the same time in the x86 version.