Dynamic memory allocation using new operator hangs
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Red Hat Linux 7.3 and MontaVista Linux
Posts: 22
Rep:
Dynamic memory allocation using new operator hangs
Hi frnds,
Im facing a strange issue which i couldnt debug why..the Dynamic memory allocation using new operator gets hang at one point.. it neither throws memory allocation failure nor exits.. it just hang forever..But this hang is occuring at very rare condition.. pls help me in debugging this.. Below is the code snippet for which im facing issue..
Here the for loop runs for 431 times and the process got hanged inside the try block forever after memalloc for 405th(not always 405th time) iteration..
Below are the debug printf lines for your reference..Here the printf and system(free -b) are debug lines added by me..
Memallocated for 404 .Size = 16 . 16.
total used free shared buffers cached
Mem: 64356352 63012864 1343488 0 1589248 43859968
-/+ buffers/cache: 17563648 46792704
Swap: 0 0 0
Memallocated for 405 .Size = 16 . 16.
total used free shared buffers cached
Mem: 64356352 63029248 1327104 0 1589248 43868160
-/+ buffers/cache: 17571840 46784512
Swap: 0 0 0
This process is a User interface process designed with Qt3.0 version and kernal version is 2.4.18..
this post would fit better into the programming section
please use the format tags for code, thanks
I do not think that the memory allocation hangs,
if something hangs than it is code executed in the constructor of QPixmap, it does a lot (loading the bitmap, possible converting,..)
if it really hangs on this line
personal note: things like
memcpy(UiPngPath,UI_PATH,MAX_LENGTH);
and
strcat(UiPngPath,BmpFileNameTable[temp])
should not be used in a QT program
there are comfortable string classes available
Qt3.0 and kernel 2.4.18. is really antic
if you really think that the mem allocation is the problem
create the picmap as Null Pixmap (new QPixmap()) and use the load method,
this might help to track down the problem to the actual problem location
Distribution: Red Hat Linux 7.3 and MontaVista Linux
Posts: 22
Original Poster
Rep:
Thanks for your reply a4z
Hi a4z,
Thanks for your ideas and comments..
Im a newbie to this website and im not aware of programming section u said..i will take care of it going forward..
Im a backend module worker.. i do not have knowledge on Qt.. This hang is observed in a embedded device running with linux..so on debugging the reason for hang i came to know that the UI process(at the above specified code) gets hanged.
So as u said (memcpy and strcat not to be used)to use string classes .. can u pls share the alternatives or any reference links??
And per suggestion, i will try using NULL Pixmap.. but what do u mean by load method?? pls share some ideas..
you may try strace on that process tho check what's happening. Is this a multithreaded app? That may cause such problems too.
You can insert fprintf lines into the code to trace it.
Depends on how much memory you are actually using...
Your print only checks the size of the array, not what the contents of that array points to.
Second, unless you disable oversubscription, the system will suspend the process as soon as it runs out of available memory (favoring paging activity). This can look like a hang, though it really isn't (it may be thrashing to death though).
Distribution: Red Hat Linux 7.3 and MontaVista Linux
Posts: 22
Original Poster
Rep:
Hi pan64,
sure.. i will try with strace.. and it is a multithreaded application running with 19 other process..
Here i have used printf..what difference will it make? fprintf will print into a file..
Distribution: Red Hat Linux 7.3 and MontaVista Linux
Posts: 22
Original Poster
Rep:
Hi jpollard ,
It is a legacy system running only with 64Mb with a limited features of linux OS hence my debugging options are limited..
Im trying to find the size of the object the process is trying to allocate i.e for QPixmap .. hence im printing sizeof(QPixmap) and then i would like to know the exact size "new" has allocated.. so im deferencing and printing sizeof(*BmpTable[temp]).. resulting both of same size i.e 16 bytes.. is my approach correct? or am i doing something wrong? pls suggest..
how to disable oversubscription? what is oversubscription meant in linux?? can u pls share some links or references as im not aware of it..
Hi jpollard ,
It is a legacy system running only with 64Mb with a limited features of linux OS hence my debugging options are limited..
Im trying to find the size of the object the process is trying to allocate i.e for QPixmap .. hence im printing sizeof(QPixmap) and then i would like to know the exact size "new" has allocated.. so im deferencing and printing sizeof(*BmpTable[temp]).. resulting both of same size i.e 16 bytes.. is my approach correct? or am i doing something wrong? pls suggest..
Not an easy one. The problem is that the QPixmap structure contains a number of pointers - one of which points to an internal data structure... Thus the size of the QPixmap reported by sizeof is only the static size - and does not include the size of the data that the structure points to. And worse, the data included in that data has pointers to still more data... The QPixmap structure is just the foundation for a tree of allocations that vary depending on what is being done. That is what makes determining the amount of memory allocated difficult to compute. All it can do is report the static size of the given structure, thus always being the same. How much memory actually gets allocated will depend on the constructor being invoked by "new" (and any constructors that constructor may cause to be invoked).
Quote:
how to disable oversubscription? what is oversubscription meant in linux?? can u pls share some links or references as im not aware of it..
Personally, I've had problems with oversubscription myself - it caused my system to either thrash to the point of unusability, or deadlocked - not sure which. In my case, it happened by a process creating a 3D model with too many objects. The rate of memory consumption was just slow enough that it did not come to the attention of the OOM kernel handler... and by the time it should have come to its attention, things were already hung. By the time I realized what happened I couldn't stop it either (I have 8GB of physical memory + 8GB swap, so it took a couple of minutes to use it all up).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.