After more research here is what I have found:
QImage::QImage(const uchar *data, int width, int height, QImage::Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr)
should be updated not only the bytesPerLine must be aligned on 32 but must be equal to "width" aligned on 32 and no larger.
Qt lib crashes when saving images created from user buffer with bytesPerLine aligned on 32 and larger than width (for example width: 300, bytesPerLine: 320) with format QImage::Format_Grayscale8
At some point during saving Qt allocates temp image with bytesPerLine that is less than the user-created image then later on this code crases:
problematic code in QT: the problem is that dest->bytes_per_line is 300 and src->bytes_per_line is 320 ; memcpy overwrites memory ->crash
dest is a temp image allocated by Qt for conversion;