Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Application crashs when I try using foreach for QList with custom class objects.
QtWS25 Last Chance

[SOLVED] Application crashs when I try using foreach for QList with custom class objects.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • shavS Offline
    shavS Offline
    shav
    wrote on last edited by
    #1

    Hi All!

    I have a problem with draw QList contents by foreach().
    I have a class:
    @
    typedef QList<MdlLogClassMethodsTimeline*> MdlLogMethodsTimelineList;
    Q_DECLARE_METATYPE(MdlLogMethodsTimelineList)

    class MdlLogMethodsInformer;
    Q_DECLARE_METATYPE(MdlLogMethodsInformer)

    class MdlLogMethodsInformer : public QObject
    {
    Q_OBJECT

        Q_PROPERTY(MdlLogMethodsTimelineList methodsTimes READ getMethodsTimes WRITE setMethodsTimes NOTIFY methodsTimesChanged)
        Q_PROPERTY(MdlLogMethodsGraph* graph READ getGraph WRITE setGraph NOTIFY graphChanged)
    
    private:
        MdlLogMethodsTimelineList methodsTimes;
        MdlLogMethodsGraph* graph;
    
    public:
        MdlLogMethodsInformer();
        MdlLogMethodsInformer(MdlLogMethodsList list);
        MdlLogMethodsInformer(const MdlLogMethodsInformer& obj);
        MdlLogMethodsInformer& operator=(const MdlLogMethodsInformer& obj);
    
        //Others methods
        QString description(QString prefix);
    
    
    
        //Getters
        inline MdlLogMethodsTimelineList getMethodsTimes(){return methodsTimes;}
        inline MdlLogMethodsGraph* getGraph(){return graph;}
    
    
    
        //Setters
        inline void setMethodsTimes(MdlLogMethodsTimelineList list){methodsTimes = list; emit methodsTimesChanged(methodsTimes);}
        inline void setGraph(MdlLogMethodsGraph* someGraph){graph = someGraph; emit graphChanged(graph);}
    
    signals:
        void methodsTimesChanged(MdlLogMethodsTimelineList list);
        void graphChanged(MdlLogMethodsGraph* someGraph);
    
    public slots:        
    

    };
    @

    When I try call description method I have crash in line when I try work with methodsTimes variable.
    The code of methods look like:
    @
    QString MdlLogMethodsInformer::description(QString prefix)
    {
    if(this == NULL)
    {
    return "NULL\n";
    }

    try
    {
        QString res = prefix + "MdlLogMethodsInformer (Object)\n";
        QString newPrefix = "|    ";
    
        res.append(prefix + newPrefix + QString("timelines (Array)\n"));
        foreach(MdlLogClassMethodsTimeline* line, methodsTimes)
        {
            res.append(line->description(prefix + newPrefix + newPrefix));
        }
        res.append(prefix + newPrefix + QString("timelines\n"));
        res.append(prefix + newPrefix + QString("graph = NULL\n"));
        res.append(prefix + "MdlLogMethodsInformer\n");
    
        return res;
    }
    catch(std::exception& e)
    {
        qDebug()<<"Exception: "<<e.what();
    }
    
    return "NULL\n";
    

    }
    @

    The crash fire on line: foreach(MdlLogClassMethodsTimeline* line, methodsTimes).
    What I do wrong? Please, help me. This method must return dump of all variables of class. Thanks for any help.

    P.S. Sorry for my English.

    Mac OS and iOS Developer

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Single step through a run of this routine and inspect the value of line. One or all of the pointers in your list is null or invalid and the actual crash will be attempting to use it at line 16.

      1 Reply Last reply
      0
      • shavS Offline
        shavS Offline
        shav
        wrote on last edited by
        #3

        [quote author="ChrisW67" date="1351639875"]Single step through a run of this routine and inspect the value of line. One or all of the pointers in your list is null or invalid and the actual crash will be attempting to use it at line 16.[/quote]

        Thanks for the reply! But I can't check my QList variable to empty state, because when I add code like this:
        @
        if(methodsTimes.isEmpty())
        {
        foreach(MdlLogClassMethodsTimeline* line, methodsTimes)
        {
        res.append(line->description(prefix + newPrefix + newPrefix));
        }
        }
        @

        Application is crashed on call methodsTimes.isEmpty() if I try getting count of items in methodsTimes - application will crashed on first call method. I can't explain but sometime this call is work. It's work in 1 from 10 start of application.

        By the way the method when I adding the items to list looks like:
        @
        MdlLogMethodsInformer::MdlLogMethodsInformer(MdlLogMethodsList list)
        {
        graph = NULL;

        if(!list.isEmpty())
        {
            methodsTimes.clear();
            int i = 0, j = 0, count = list.count();
            while(i < count)
            {
                MdlLogClassMethod* method = list[i];
        
                if(method != NULL && method->getAction() == QString("StartCall"))
                {
                    QString methodName = method->getElementId();
                    j = i+1;
                    bool endElemFounded = false;
                    while(j < count)
                    {
                        MdlLogClassMethod* endMethod = list[j];
                        if(methodName == endMethod->getElementId() && endMethod->getAction() == QString("EndCall"))
                        {
                            MdlLogClassMethodsTimeline* line = new MdlLogClassMethodsTimeline(method, endMethod);
                            if(line != NULL)
                            {
                                methodsTimes.insert(methodsTimes.count(), line);
                            }
                            endElemFounded = true;
        
                            break;
                        }
        
                        ++j;
                    }
        
                    if(!endElemFounded)
                    {
                        methodsTimes.insert(methodsTimes.count(), new MdlLogClassMethodsTimeline(method, NULL));
                    }
                }
                ++i;
            }
        }
        
        qDebug()<<"methodsTimelines count: "<<methodsTimes.count(); //<< In this line application crashed
        

        }
        @

        I was add a line
        @ qDebug()<<"methodsTimelines count: "<<methodsTimes.count(); @

        to this method and application now is crashed on this line.

        How I can check correction data in my QList variable if when I try to use isEmpty method application will crash?

        Mac OS and iOS Developer

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          [quote author="shav" date="1351663862"]How I can check correction data in my QList variable if when I try to use isEmpty method application will crash?[/quote]Run your application in a debugger.

          It will automatically place you at the position your application crashed. If not, put a breakpoint right before the call to QList::isEmpty() and step through.

          I would valgrind the application as well (or any other memory corruption detection tool).

          1 Reply Last reply
          0
          • shavS Offline
            shavS Offline
            shav
            wrote on last edited by
            #5

            [quote author="Lukas Geyer" date="1351670161"][quote author="shav" date="1351663862"]How I can check correction data in my QList variable if when I try to use isEmpty method application will crash?[/quote]Run your application in a debugger.

            It will automatically place you at the position your application crashed. If not, put a breakpoint right before the call to QList::isEmpty() and step through.

            I would valgrind the application as well (or any other memory corruption detection tool).[/quote]

            Thanks! I found the problem.

            Mac OS and iOS Developer

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved