Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2007
Qt 4.2.2 and memory problems


Message 1 in thread

Hi,

I am writing a small serverless chat and file sharing application for my 
local lan. Im this application, obviously I need to open a lot of windows 
and sockets, etc. In such a situation, handling the deletion of all such 
objects when their job is over is a very difficult task for me. Is there a 
way to specify automatic deletion of QTcpSocket, QWidget, QStringList, 
etc. when some event occurs?

I also ran into problems when the destructor gets called while in some 
other thread, execution of a member function is going on. I just put an 
'integer' variable called 'lock' to stop the destructor call till we're 
executing in a function, is there a better way of locking?

Even QPixmaps use up a lot of memory if constructed every time you need an 
icon. Finally I had to include a data member as a QPixmap in order to 
avoid frequent creation. Is there a way to ensure that the memory used by 
the pixmap gets cleared once its no longer in use?

In short, I just want to understand how can I manage my memory more 
efficiently for my application.

Regards,
-- 
 [ signature omitted ] 

Message 2 in thread

> I also ran into problems when the destructor gets called while in some 
> other thread, execution of a member function is going on. I just put an 
> 'integer' variable called 'lock' to stop the destructor call till we're 
> executing in a function, is there a better way of locking?

YES! Use QThread::wait() in the destructors of your threads. That way the 
destructor will finish whenever QThread::run() comes to an end. Of course 
you have to come up with a way to quit the run() method. This way you can 
smoothly terminate your thread doing everything that needs to be done 
first ;-)

Hope I got your question right :)

Regards,
Malte

--
 [ signature omitted ] 

Message 3 in thread

> YES! Use QThread::wait() in the destructors of your threads. That way the
> destructor will finish whenever QThread::run() comes to an end. Of course
> you have to come up with a way to quit the run() method. This way you can
> smoothly terminate your thread doing everything that needs to be done
> first ;-)

I didn't create any explicit threads in the code. I have a QTimer to a 
function for remove all sockets that are done with their job. 
Unfortunately, even after completion of the job, the socket class does 
some cleanup activities. When it is doing so, the cleanup function calls 
delete for that object, and causes segmentation faults. Now, I do a 
locked++ in the beginning of every member function and locked-- before 
exiting. So, I avoid calling delete for objects which have locked>0. Is 
there a better way?

-- 
 [ signature omitted ] 

Message 4 in thread

> I didn't create any explicit threads in the code. I have a QTimer to a 
> function for remove all sockets that are done with their job. 
> Unfortunately, even after completion of the job, the socket class does 
> some cleanup activities. When it is doing so, the cleanup function calls 

> delete for that object, and causes segmentation faults. Now, I do a 
> locked++ in the beginning of every member function and locked-- before 
> exiting. So, I avoid calling delete for objects which have locked>0. Is 
> there a better way?

I have a hard time understanding what you are doing exactly. Is it 
possible for you to post some code snipplets or some pseudo-code?

Regards,
Malte

--
 [ signature omitted ] 

Message 5 in thread

> I have a hard time understanding what you are doing exactly. Is it
> possible for you to post some code snipplets or some pseudo-code?

Ok, its like this. There are two classes, Communicator and ClientHandler. 
Communicator is like a central controlling thing. It has a QTimer that 
calles 'refreshCommunication' function. Communicator is derived from 
QTcpServer. It listens for new clients, when a new one connects, it 
creates a 'ClientHandler' object to handle it. All these ClientHandler 
objects are appeneded to a QList in Communicator.

Now, the 'refreshCommunication' function goes through the QList of 
ClientHandler objects and calls 'delete' for those which have status as 
'H_DONE' or 'H_ERROR'.

In ClientHandler, if the job is done successfully, it sets status as 
H_DONE and for error, H_ERROR. Now, when ClientHandler is carrying out 
exection of some member function (after it has set the status to be 
'done' or 'error'), the 'refreshCommunication' function calls destructor 
and then all subsequent operations in ClientHandler's currently executing 
member function fail/seg fault.

I added a data member 'locked' in ClientHandler (init to 0). While 
entering any member function, I do locked++ and on returning locked--. 
This way, in 'refreshCommunication' I don't delete those ClientHandler 
objects having locked>0.

Is there a better way to lock ClientHandler, to somehow determine if any 
of its member functions are currently under execution?

Hope now its clearer.

Regards,
-- 
 [ signature omitted ] 

Message 6 in thread

> Is there a better way to lock ClientHandler, to somehow determine if any
> of its member functions are currently under execution?

The one thing that comes to mind is if your ClientHandlers are QObjects, you could
use "deleteLater()" on them to defer their deletion until after the event queue
clears up.

Caleb

--
 [ signature omitted ]