0

I have a method, which generates a huge QVector<uchar> (SIZE: 354792000), and memmory is allocated/freed up dynamically, for convenience I use QScopedPointer:

void someMethod(int SIZE) {
  chVec.reset(new QVector<uchar>(SIZE));
       /*doing some stuff with an array...*/
  chVec.data()->clear();
  chVec.data()->squeeze();
}

Creating arrays of small size - no problem, you can also create one array of large size, but when I call the method again, I get the error "Invalid parameter passed to C runtime function". It went through a debugger, an error occurs when trying to allocate space for a new array:

template <typename T>
QVector<T>::QVector(int asize, const T &t)
{
    Q_ASSERT_X(asize >= 0, "QVector::QVector", "Size must be greater than or equal to 0.");
    if (asize > 0) {
        d = Data::allocate(asize);
        Q_CHECK_PTR(d); /*<------ Error occured*/
        d->size = asize;
        T* i = d->end();
        while (i != d->begin())
            new (--i) T(t);
    } else {
        d = Data::sharedNull();
    }
}

Data::allocate(asize); :

Q_REQUIRED_RESULT static QTypedArrayData *allocate(size_t capacity,
            AllocationOptions options = Default)
    {
        Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
        return static_cast<QTypedArrayData *>(QArrayData::allocate(sizeof(T),
                    Q_ALIGNOF(AlignmentDummy), capacity, options));
    }     

What could it be? A memory leak (it is impossible to allocate such a large chunk of contiguous memory again, since the previous call did not free the memory?)? ... I just can’t understand what the problem is, before leaving the method scope, I even manually clean the array and free the memory ... I would be grateful for any advice

Cataract
  • 31
  • 1
  • 5
  • Isn't it because of a fragmented memory? what is your process virtual memory space? did you investigate the memory allocation footprint of your process? – Soheil Armin Nov 03 '19 at 14:29
  • @Soheil Thank you for your reply. I've solved my problem by changing mingw32 to mingw64, despite the fact that my program did not exceed the size of 2 GB - I had a badAlloc trouble – Cataract Nov 03 '19 at 15:10

1 Answers1

0

The problem was solved by changing mingw32 to mingw64

Cataract
  • 31
  • 1
  • 5
  • so was it a memory fragmentation problem? surely, 2GB of memory could be fragmented earlier. – Soheil Armin Nov 03 '19 at 15:14
  • @Soheil, well, it seems to be the problem, but I can't say this for sure.. I used resourses monitor to find out how much memory does my programm use - it's about 200 mb, after vector allocation it become 600 mb, and when the memory was freed it become 200 mb back. My guess is: qt allocates/frees space during the lifetime of the program, and this can lead to the fact that it will be impossible to allocate a large portion of continuous memory for a singly linked list... but that are only my thoughts (sorry for my English) – Cataract Nov 03 '19 at 15:28
  • Memory fragmentation can occur on any memory consumption. That's not about how much used or allocated memory. It's about how memory is allocated through the virtual memory space. So, I assume that it was a memory fragmentation. – Soheil Armin Nov 03 '19 at 15:49
  • @Soheil I've just read about this, and I assume - you are right. Thank you. Is there any way to manipulate fragmentation (cluster size?) in runtime? Or the only solution is to reserve some extra space and don't free it until the program destructor called? – Cataract Nov 03 '19 at 16:04
  • I'm not sure if it works. Reserving a contiguous portion of memory for your QVector in the early stages of execution of your program will help. And it's about the ***virtual memory space*** which is managed by your OS, not the physical RAM. – Soheil Armin Nov 03 '19 at 16:28
  • @Soheil "Reserving a contiguous portion of memory for your QVector in the early stages of execution of your program will help." It's the same as I just wrote - " reserve some extra space and don't free it until the program destructor called". The case is - I don't know how much space will I need. I could reuse this allocated memory, but holding a big block of memory during all lifecircle of program - is not a good idea. I think I should read more about mingw, allocating memory in qt-framework and large-address-aware. – Cataract Nov 03 '19 at 17:03