Results 1 to 16 of 16

Thread: Problems with threads and windows

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems with threads and windows

    Hi all, I post the code where I interact with the thread that creates the thumbnails of the images becuase the other thread uses a lot of irrelevant functions. The problem with this thread is that if I move the scrollbars of the QWidgetList when the thread is creating the thumbnails (and the main thread updating the list) the program chrashes. That's the code of the 'run' method of the thread:
    void CreadorFotosPetitesThread::run()
    {
    QImage fotoImage;

    uint n = llista.count();

    for (uint i = 0; i < n && !abortar; i++)
    {
    if (fotoImage.load(dir + llista[i]))
    {
    fotoImage = fotoImage.scaled(TAM_ICON_HOR,
    TAM_ICON_VER, Qt::KeepAspectRatio,
    Qt::FastTransformation);

    emit fotoPetitaCreada(fotoImage, i, dir);
    }
    }

    emit fiCreacio();
    }
    In the method where I call the start function of the thread, I pass the list of files ('llista') and the directory ('dir') which it creates the thumbnails. And the following code is the slot that I connect with the signal 'fotoPetitaCreada' in the main thread:
    void PaginaResultats::actualitzarFotosPetitesList(const QImage &fotoImage, int index, const QString &pathDir)
    {
    QListWidgetItem *fotoPetitaItem = fotosPetitesList -> item(index);

    QString pathDirActual = ((DirectoriTreeWidgetItem *)
    (subDirsTreeWidget -> currentItem())) -> pathDirectori();

    if (!fotoPetitaItem || pathDir != pathDirActual) return;

    fotoPetitaItem -> setIcon(QPixmap::fromImage(fotoImage));
    }
    'fotosPetitesList' is the QListWidget and, before the thread is started, I fill the list to set the text of the QListWidget items with the name of the files and an inicialization icon that will be replaced later with the icon created with the image that provides the thread.

    I hope that this will help to find the problem. If there's some question with this code feel free to ask me here. Thanks
    Last edited by SkripT; 12th January 2006 at 12:30.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Problems with threads and windows

    Quote Originally Posted by SkripT
    void PaginaResultats::actualitzarFotosPetitesList(const QImage &fotoImage, int index, const QString &pathDir)
    You pass a reference to that slot and since that slot is in another thread it will be delivered through an event, but the object to which this reference refers to will be overwritten in the same time by the original thread. This might lead to serious troubles.

    Try passing that QImage and QString by value (they use implicit sharing, so there won't be any problems).

  3. #3
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems with threads and windows

    Yes this could cause an error but I made it this way because if you see the example /examples/threads/mandelbrot passes a reference to the generated image too. This is because the connection between the signals and slots is queued because are different threads, as the documentation explains. This means, I think, that the parameters of the signal are copied in a queue before the slot is called. But depending in the time that the parameters are copied in the queue could cause an error, so why an example of the doucmentation are doing it wrong?
    Last edited by SkripT; 12th January 2006 at 16:55.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Problems with threads and windows

    Quote Originally Posted by SkripT
    Yes this could cause an error but I made it this way because if you see the example /examples/threads/mandelbrot passes a reference to the generated image too.
    I've checked that example and Qt sources and it looks like emit makes a copy of those parameters, even when you pass then as a reference.

    I hope you have noticed this:
    With queued connections, Qt must store a copy of the arguments that were passed to the signal so that it can pass them to the slot later on. Qt knows how to take of copy of many C++ and Qt types, but QImage isn't one of them. We must therefore call the template function qRegisterMetaType() before we can use QImage as parameter in queued connections.

  5. #5
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems with threads and windows

    Yes, I make a qRegisterMetaType() of QImage and QString before I connect the signal with the slot. Any other suggestion please?
    Last edited by SkripT; 13th January 2006 at 00:05.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Problems with threads and windows

    Did you try to run that application from debugger? Could you post the backtrace?

  7. #7
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems with threads and windows

    Sorry for my ignorance but could you please tell me how to run the debugger in the commad line to obtain the backtrace? Thanks

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Problems with threads and windows

    Quote Originally Posted by SkripT
    how to run the debugger in the commad line to obtain the backtrace?
    If you use MinGW, it should be:
    shell Code:
    1. C:\...> gdb prog.exe
    2. (gdb) run
    3. <program crashes>
    4. (gdb) bt
    5. <backtrace>
    6. (gdb) quit
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems with threads and windows

    thanks a lot i'm going to try it.

  10. #10
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems with threads and windows

    Hi, here's the backtrace as a result of a program crash:
    Program received signal SIGSEGV, Segmentation fault.
    [Switching to thread -277..........]
    0x1012c44b in _size_of_stack_reserve__ ()
    and the message from the program drwatson of windows is that the program has made an illegal acess to memory.
    As I think, crashes as a result of an overload of the stack. I guess that's because I am working with some QImages at the same time and I think that could be better to define the images in dynamic memory. I will try it.
    Last edited by SkripT; 13th January 2006 at 23:55.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Problems with threads and windows

    Quote Originally Posted by SkripT
    Hi, here's the backtrace as a result of a program crash
    It's rather short. Wasn't there more output?

    As I think, crashes as a result of an overload of the stack. I guess that's because I am working with some QImages at the same time and I think that could be better to define the images in dynamic memory.
    AFAIK QImage stores data on the heap.

  12. #12
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems with threads and windows

    Hi again, gdb alerts me with two warnings that I tried to re-register class QString because I was trying to do a qregistertyper of QString. I have deleted the instruction and seems that the program not crashes, in the short time that I have tested it. Could I think that this was the problem?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.