LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-24-2014, 01:52 AM   #1
ArunkumarRavi
LQ Newbie
 
Registered: Jul 2014
Location: Chennai,Tamilnadu,India
Distribution: Red Hat Linux 7.3 and MontaVista Linux
Posts: 22

Rep: Reputation: Disabled
Post 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..

for (temp = 0;temp < NUMOFPIXMAS;temp++)
{
try
{
memcpy(UiPngPath,UI_PATH,MAX_LENGTH);
BmpTable[temp]= new QPixmap(strcat(UiPngPath,BmpFileNameTable[temp]));
printf("Memallocated for %d .Size = %d . %d.",ntemp,sizeof(QPixmap),sizeof(*BmpTable[temp]));
fflush(stdout);
} catch (...)
{
mReportMemoryAlarm();
}
system("free -b");
}

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..
 
Old 07-24-2014, 02:37 PM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
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
 
Old 07-24-2014, 11:27 PM   #3
ArunkumarRavi
LQ Newbie
 
Registered: Jul 2014
Location: Chennai,Tamilnadu,India
Distribution: Red Hat Linux 7.3 and MontaVista Linux
Posts: 22

Original Poster
Rep: Reputation: Disabled
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..
 
Old 07-25-2014, 01:08 AM   #4
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
here you find the documentation to qpixmap
http://doc.qt.digia.com/3.0/qpixmap.html

check the constructors, you are loading form char*
you should load using http://doc.qt.digia.com/3.0/qstring.html
if your strings are file pathes and not a xmp image
do not forget to read the rest of the docs, or at least as much as possible
http://doc.qt.digia.com/3.0/index.html
 
Old 07-25-2014, 01:48 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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.
 
Old 07-25-2014, 05:59 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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).
 
Old 07-28-2014, 02:00 AM   #7
ArunkumarRavi
LQ Newbie
 
Registered: Jul 2014
Location: Chennai,Tamilnadu,India
Distribution: Red Hat Linux 7.3 and MontaVista Linux
Posts: 22

Original Poster
Rep: Reputation: Disabled
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..
 
Old 07-28-2014, 02:10 AM   #8
ArunkumarRavi
LQ Newbie
 
Registered: Jul 2014
Location: Chennai,Tamilnadu,India
Distribution: Red Hat Linux 7.3 and MontaVista Linux
Posts: 22

Original Poster
Rep: Reputation: Disabled
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..
 
Old 07-28-2014, 02:48 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
printf or fprintf is almost the same. I just wanted to suggest you to insert more lines to print info.
 
Old 07-28-2014, 05:50 AM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by ArunkumarRavi View Post
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..
A discussion of oversubscription is at
http://opsmonkey.blogspot.com/2007/0...vercommit.html

Some discussion of the problems it causes (as well as how to control it):
http://www.win.tue.nl/~aeb/linux/lk/lk-9.html

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).

BTW, the entire QPixmap structure is viewable at:
http://techdoc.kvindesland.no/linux/qt2/qpixmap-h.html

You can see the embedded pointers to data in that.

Last edited by jpollard; 07-28-2014 at 06:01 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dynamic memory allocation in assembly hda7 Linux - Software 9 10-28-2009 08:25 AM
Dynamic Memory Allocation jvff Programming 1 09-05-2005 05:01 AM
dynamic memory allocation failure guam Programming 4 04-13-2005 09:16 AM
pointers and dynamic memory allocation deveraux83 Programming 2 01-24-2004 10:35 AM
Dynamic Memory Allocation query dhanakom Programming 2 07-21-2003 02:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration