Results 1 to 6 of 6

Thread: runJavaScript() crashes my program.

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default runJavaScript() crashes my program.

    I am trying to extract some data from an HTML document but for some reason my program crashes because of the code below. the program crashes when I capture the variable info_data in my lambda function. any help would be appreciated.

    Qt Code:
    1. QString WebView::retData_Html()
    2. {
    3. QVariant info_data;
    4.  
    5. this->page()->runJavaScript("function retContactDetails(){"
    6. "var x = document.getElementsByClassName(\"result\");"
    7.  
    8. "var detailsArray = [];"
    9. "var companyName;"
    10. "var contactNumer;"
    11. "var address;"
    12.  
    13. "var i;"
    14. "for(i = 0; i < x.length; i++){"
    15.  
    16. "var child_nodes = x[i].children;"
    17.  
    18. "var j;"
    19. "for(j = 0; j < child_nodes.length; j++){"
    20.  
    21. "if(child_nodes[j].className == \"resultName\"){"
    22. "companyName = child_nodes[j].firstElementChild.innerHTML;}"
    23.  
    24. "else if(child_nodes[j].className == \"resultAddress\"){"
    25. "address = child_nodes[j].textContent;}"
    26.  
    27. "else if(child_nodes[j].className == \"resultContact\"){"
    28. "contactNumer = child_nodes[j].firstElementChild.textContent;}"
    29.  
    30. "}"
    31. "detailsArray.push(companyName);"
    32. "detailsArray.push(address);"
    33. "detailsArray.push(contactNumer);"
    34.  
    35. "}"
    36.  
    37. "return detailsArray.toString();"
    38. "}"
    39. "retContactDetails();",[&info_data](const QVariant &infoData){info_data = infoData/*qDebug() << infoData.toString() <<endl*/;});
    40.  
    41. return info_data.toString();
    42.  
    43. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: runJavaScript() crashes my program.

    & in the capture list means the variable is captured by reference.

    The variable in question is allocated on the stack, it ceases to exist when the scope of the function ends.

    So you lambda access memory that is no longer accessible.

    Either capture a heap allocated variable or one that is a member of the class.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ayanda83 (21st November 2016)

  4. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: runJavaScript() crashes my program.

    Thanks, wouldn't have known that, still new to C++ 11 concepts.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: runJavaScript() crashes my program.

    Also, since the method needs a callback instead of having a return value, it might call the lambda asychronously.
    So your return value might sometimes or always be an empty string right now.

    Cheers,
    _

  6. #5
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: runJavaScript() crashes my program.

    I've noticed that and I've been wondering what courses it. Is there a better solution then? One that is not going to return an empty string.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: runJavaScript() crashes my program.

    Well, theoretically a nested event loop.

    But better would of course be to not force a synchronous behavior onto an asynchronous operation.

    Cheers,
    _

Similar Threads

  1. Getting image directly with runJavaScript
    By Fred in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2016, 15:37
  2. Using exceptions in Qt 5.1.1 crashes the program
    By themagician in forum Newbie
    Replies: 0
    Last Post: 24th November 2013, 18:34
  3. Program crashes on 64-bit systems
    By WereWind in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2013, 10:29
  4. Program crashes
    By Fallen_ in forum Qt Programming
    Replies: 49
    Last Post: 20th September 2010, 01:41
  5. program crashes (QtTestRunner)
    By fmariusd in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2008, 09:27

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.