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

Qt-interest Archive, May 2007
problems when this function is called several times


Message 1 in thread

Hi

This function reads a scenario file if the user clicks stop button the 
scenario is reloaded. but if the user reloads 2-3 times the file takes 
twice as much time to reload as if the file is becoming bigger each time 
it this function is called.

when stop = true;


void Scen_Parser::open_scenfile()
{
     clearScenario();
     guiObject->progressbar->show();
     guiObject->actionStart->setDisabled(1);

     if (stoped == false) {
         QSettings  settings;
         settings.setValue( "lastpath",QDir( fileName ).absolutePath() 
);//this will remember previous opened directories.
         fileName = QFileDialog::getOpenFileName(0, "Open Scenario 
File", settings.value("lastpath").toString());

     }
     QFile file(fileName);

     int read = 0;
     int size = file.size();
     cout <<file.size()<<endl;
     guiObject->QMainWindow::setWindowTitle("NEMAN:          " +fileName);
     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
         QMessageBox::critical(guiObject,"Error!", "Not a valid scenario 
file.");
         return;

     }
     while (!file.atEnd()) {
         QString line = file.readLine();
         read += line.length();
         QString found;

/* # nodes: 10, pause: 3.00, max speed: 3.00, max x: 1000.00, max y: 
1000.00 */
         static QRegExp 
rx("#\\s+\\w*:\\s+(\\d+),\\s+\\w*:\\s+([-+]?[0-9]\\.?[0-9]+),\\s+\\w*\\s+\\w*:\\s+([-+]?[0-9][0-9]?\\.?[0-9]+),\\s+\\w*\\s+\\w*:\\s+([-+]?[0-9]*\\.?[0-9]+),\\s+\\w*\\s+\\w*:\\
s+([-+]?[0-9]*\\.?[0-9]+)");

         if (rx.indexIn(line) != -1) {
             isScenFile = true; //checks if ns2 scenario file is opened.
             found = rx.cap(0);
             numNodes = rx.cap(1).toInt();
             pause = rx.cap(2).toDouble();

             max_speed = rx.cap(3).toDouble();
             scenMax_x = rx.cap(4).toDouble();
             scenMax_y = rx.cap(5).toDouble();
         }
/* # nodes: 10, pause: 3.00, max speed: 3.00, max x = 1000.00, max y: 
1000.00 */
         static QRegExp 
rx_2("#\\s+\\w*:\\s+(\\d+),\\s+\\w*:\\s+([-+]?[0-9]\\.?[0-9]+),\\s+\\w*\\s+\\w*:\\s+([-+]?[0-9][0-9]?\\.?[0-9]+)\\s+\\w*\\s+\\w*\\s+\\=\\s+([-+]?[0-9]*\\.?[0-9]+),\\s+\\w*\\s+
\\w*:\\s+([-+]?[0-9]*\\.?[0-9]+)");

          if (rx_2.indexIn(line) != -1) {
              isScenFile = true; //checks if mkdist created scenario 
file is opened.
              found = rx_2.cap(0);
             numNodes = rx_2.cap(1).toInt();
             pause = rx_2.cap(2).toDouble();

             max_speed = rx_2.cap(3).toDouble();
             scenMax_x = rx_2.cap(4).toDouble();
             scenMax_y = rx_2.cap(5).toDouble();
          }

         /* $node_(0) set X_ 305.182504346120 */
         static QRegExp 
rx2("(\\w*\\((\\d+\\)))\\s+\\w*\\s+\\w*\\s+([-+]?[0-9].*)");
         if (rx2.indexIn(line) != -1){
             QString name = rx2.cap(1);
             QString found2 = rx2.cap(0);
             x = rx2.cap(3).toDouble();
             if (x > 0.0 )//we are not interested in Z only X, Y.
                 myVector.push_back(x);
         }
         static QRegExp 
sensor("\\w*\\_\\((\\d+)\\)\\s+set\\s+type\\s+sensor");
         if (sensor.indexIn(line) != -1){
             sensorVector.push_back(1);
         }
   /* $god_ set-dist 0 1 16777215 */
         static QRegExp 
rx3("^\\$god_?\\s+\\w*-\\w*\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");

         if(rx3.indexIn(line) != -1){
             QString test = rx3.cap(0);
             node1.push_back(rx3.cap(1).toInt());
             node2.push_back(rx3.cap(2).toInt());
             reachAble.push_back(rx3.cap(3).toInt());

         }

/*$ns_ at 3.000000000000 "$node_(0) setdest 866.099161363353 
753.025172657621 0.751030275648"
  */
         static QRegExp 
rx4("^\\$\\w*\\s+\\w*\\s+([-+]?[0-9]*\\.?[0-9]+)\\s+\\\"\\$\\w*\\((\\d+)\\)\\s+\\w*\\s+([-+]?[0-9]*\\.?[0-9]+)\\s+([-+]?[0-9]*\\.?[0-9]+)\\s+([-+]?[0-9]*\\.?[0-9]+)");
         if (rx4.indexIn(line) != -1){

             time.push_back(rx4.cap(1).toDouble());
             //cout <<"CAPTURE: "<<rx4.cap(1).toDouble()<<endl;
             nodeNr.push_back(rx4.cap(2).toInt());
             posX.push_back(rx4.cap(3).toDouble());
             posY.push_back(rx4.cap(4).toDouble());
             speed.push_back(rx4.cap(5).toDouble());

         }
         /* $ns_ at 41.329826354576 "$god_ setdest 2 3 2" */
         static QRegExp 
rx5("^\\$\\w*\\s+\\w*\\s+([-+]?[0-9]*\\.?[0-9]+)\\s+\\\"\\$\\w*\\s+\\w*\\-\\w*\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");

         if (rx5.indexIn(line) != -1){

             destTime.push_back(rx5.cap(1).toDouble());
             destNode1.push_back(rx5.cap(2).toInt());
             destNode2.push_back(rx5.cap(3).toInt());
             destReachable.push_back(rx5.cap(4).toInt());
         }

         static QRegExp 
rx6("^\\#\\s+data_range:\\s+([-+]?[0-9]*\\.?[0-9]+)");
         if (rx6.indexIn(line) != -1){
             comRange = rx6.cap(1).toDouble();
         }

         static QRegExp 
rx7("^\\#\\s+broadcast_range:\\s+([-+]?[0-9]*\\.?[0-9]+)");
         if (rx7.indexIn(line) != -1){
             brRange = rx7.cap(1).toDouble();
         }

         /* this regexp will match for sendmsg part collecting time 
senderNode receiverNode message Id message Port and message */
         static QRegExp 
sendmsg("^\\$\\w*\\s+\\w*\\s+([-+]?[0-9]*\\.?[0-9]+)\\s+\\\"\\$\\w*\\((\\d+)\\)\\s+sendmsg\\s+\\w*=((?:[0-9]{1,3}\\.){3}[0-9]{1,3})\\s+\\w*=(\\d+)\\s+\\w*=(\\d+)\\s+\\w*=(\\'\
\w*\\')");
         if (sendmsg.indexIn(line) != -1){
             msgTime.push_back(sendmsg.cap(1).toDouble());
             tapFromIp.push_back(sendmsg.cap(2));
             tapToIp.push_back(sendmsg.cap(3));
             msgPort.push_back(sendmsg.cap(4).toInt());
             msgId.push_back(sendmsg.cap(5).toInt());
             message.push_back(sendmsg.cap(6));

         }

          //display error message if other files than accepted scenario 
files are opened.
         if (file.atEnd() && isScenFile == false){
             QMessageBox::critical(guiObject,"Error!", "Not a valid 
scenario file.");
             break;
         }
         QApplication::processEvents();//thread the gui, when busy.

         guiObject->progressbar->setRange(0,size);
         guiObject->progressbar->setValue(read);
     }

     guiObject->progressbar->hide();
     guiObject->actionStart->setDisabled(0);

     prepareNodes();
     stoped = false;
     isScenFile = false;
     if (guiObject->hideRange->isChecked())
         hideOrShowRanges();
     if (guiObject->hideLinks->isChecked())
         hideOrShowLinks();

     if (guiObject->fitInBox->isChecked())
         fitIn();
}

--
 [ signature omitted ] 

Message 2 in thread

suleiman wrote:
> This function reads a scenario file if the user clicks stop button the
> scenario is reloaded. but if the user reloads 2-3 times the file takes
> twice as much time to reload as if the file is becoming bigger each
time
> it this function is called.

I'd search for a memory leak (maybe in clearScenario() - does it really
free everything?). So its not a matter of the file becoming bigger but
rather system resources becoming less.

Cheers,
Peter


--
 [ signature omitted ] 

Message 3 in thread

On 08.05.2007 15:36, Peter Prade wrote:
> suleiman wrote:
>> This function reads a scenario file if the user clicks stop button the
>> scenario is reloaded. but if the user reloads 2-3 times the file takes
>> twice as much time to reload as if the file is becoming bigger each
> time
>> it this function is called.
> 
> I'd search for a memory leak (maybe in clearScenario() - does it really
> free everything?). So its not a matter of the file becoming bigger but
> rather system resources becoming less.
> 
> Cheers,
> Peter
> 
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

Yes i think iam freeing everything
Here is clearScenario() which frees the nodes and the nodes free all 
their items because of the parent object relationship.

void Scen_Parser::clearScenario()
{
  /* before we open new scenario we remove previous if any */
     if(!mapNodes.isEmpty()){

         myVector.clear();
         node1.clear();
         node2.clear();
         reachAble.clear();
         posX.clear();
         posY.clear();
         time.clear();
         nodeNr.clear();
         speed.clear();
         destTime.clear();
         destNode1.clear();
         destNode2.clear();
         sensorVector.clear();

         QList<QGraphicsItem *> list = scene->items();
         QList<QGraphicsItem *>::Iterator it = list.begin();
         for (; it != list.end(); ++it) {
             if ( *it && (*it)->parentItem() == 0 ){
                 scene->removeItem(*it);

             }
         }
         /* deletes the objects allocated in the heap */
         QList<Node *> deletingNodes;
         foreach(Node *node, mapNodes){ deletingNodes.append(node); }
         QList<Node *>::Iterator iter = deletingNodes.begin();
         for (; iter != deletingNodes.end(); ++iter) {
             delete  *iter;
         }
         mapNodes.clear();

     }
}

--
 [ signature omitted ] 

Message 4 in thread

suleiman wrote:
> Yes i think iam freeing everything
> Here is clearScenario() which frees the nodes and the nodes free all 
> their items because of the parent object relationship.
> 
>         QList<QGraphicsItem *> list = scene->items();
>         QList<QGraphicsItem *>::Iterator it = list.begin();
>         for (; it != list.end(); ++it) {
>             if ( *it && (*it)->parentItem() == 0 ){
>                 scene->removeItem(*it);
> 
>             }
>         }

It seems that you are not deleting the graphics item from the heap.
removeItem() does not delete the item from the heap (as documented)
http://doc.trolltech.com/4.2/qgraphicsscene.html#removeItem


-- 
 [ signature omitted ] 

Message 5 in thread

On 5/8/07, suleiman <suleimaj@xxxxxxxxxx> wrote:
> Hi
>
> This function reads a scenario file if the user clicks stop button the
> scenario is reloaded. but if the user reloads 2-3 times the file takes
> twice as much time to reload as if the file is becoming bigger each time
> it this function is called.

Is this a slot connected to a button? If the user clicks the action
button a second time before the first run finishes, you could have two
instances of the function running at once (cooperative multitasking
via QApplication::processEvents()).

Also, have you tried profiling the code on the fast and slow runs to
see what's actually causing the slowdown?
-- 
 [ signature omitted ] 

Message 6 in thread

On 08.05.2007 16:24, suleiman wrote:
> On 08.05.2007 16:19, Andrew Medico wrote:
>> On 5/8/07, suleiman <suleimaj@xxxxxxxxxx> wrote:
>>> Hi
>>>
>>> This function reads a scenario file if the user clicks stop button the
>>> scenario is reloaded. but if the user reloads 2-3 times the file takes
>>> twice as much time to reload as if the file is becoming bigger each time
>>> it this function is called.
>>
>> Is this a slot connected to a button? If the user clicks the action
>> button a second time before the first run finishes, you could have two
>> instances of the function running at once (cooperative multitasking
>> via QApplication::processEvents()).
>>
>> Also, have you tried profiling the code on the fast and slow runs to
>> see what's actually causing the slowdown?
> The slow down is because the function is called more than once, the file 
> is therefore read many times.
> 
> So i want to figure out what is making the function beeing called so 
> many times when its only supposed to be called once.
> 
> Yes it is a slot, but another function is using this function as slot 
> when the open file button is clicked.
> 

--
 [ signature omitted ]