QPixmap VS QImage, X server problem
I’m developing an application (for LINUX) that show some images downloaded from the web. The application works fine but I found a strange behavior of the X server.
The part of code is:
- void VisualizzazioneWidget::immagineArrivata(bool esito)
This function is execute every second.
I’v seen that in a PC without a dedicated graphic card the X server process memory usage grow up endless. While in a PC with a dedicated graphic card it doesn’t append.
To solve the problem I repalced QPixmap with QImage and all works fine:
I also replaced all other occurrence of QPixmap with QImage in other part of the application.
I seen this in QT documentation:
- Note also that QPixmap, unlike QImage, may be hardware dependent. On X11, Mac and Symbian, a QPixmap is stored on the server side while a QImage is stored on the client side (on Windows, these two classes have an equivalent internal representation, i.e. both QImage and QPixmap are stored on the client side and don't use any GDI resources).
but why there is no problem if I have a dedicated graphics card? Is it a bug?
I found this link that talk about X server and memory leak:
There is a part where it says:
- Who Frees the Memory and How?
- Now that you've determined when dynamic memory is allocated by the X libraries, the next questions should be:
- 1. does the application have to free the memory?
- 2. if yes, when can it safely free the memory?
- 3. what function should be used to free the memory?
- Unfortunately, the X and, especially, the Motif documentation is not always clear about which case applies to a particular programming interface. In this section, I'll discuss some general guidelines in these areas. Next month's column will cover specific programming interfaces in more specific detail.
- Freeing dynamic memory that shouldn't be freed can lead to many problems in the X libraries, including corrupting memory in use by X, freeing memory twice, and writing to invalid memory. Freeing memory with the wrong function can cause it to be improperly freed, including not freeing all the allocated memory.
does QPixmap::ShareMode have anything to do with this. It says on x11, you have to explicitly delete the pixmap handle. how would one use it?
also, not related to leak, but I’ve found QImageReader to be more efficient in terms of memory footprint, you might cross check.. below from qt docs:
- QImageReader is a specialized class which gives you more control when reading images. For example,
- you can read an image into a specific size by calling setScaledSize(), and you can select a clip rect,
- effectively loading only parts of an image, by calling setClipRect(). Depending on the underlying support
- in the image format, this can save memory and speed up loading of images