I have a problem to translate a C program to QT GUI.



  • Hi, i have a problem with a C program when i try to translate as a GUI with QT.
    I made the following the main() function from C , with a while(1) {} loop inside, it is named mainloopC() function and i call this one at the Constructor, the program works but the GUI it is not setuped, that means i dont have a GUI.

    Then i tried to put a PushButton to call the function mainloopC(), now i have the GUI but if i push the button i have a segmentation error in this line @if((data = ((unsigned char *) malloc(data_len)))== NULL) exit(1);@
    I know that malloc() it is not good in C++, but why in Constructor the program runs and if i use a push button the program corrupts?
    The code it's huge, and i didnot post it but if you have a suggestion i can post parts of the program.


  • Moderators

    Hi,

    [quote author="xmaze" date="1422978617"]I made the following the main() function from C , with a while(1) {} loop inside, it is named mainloopC() function[/quote]To use a GUI, you need an event loop. You cannot have an infinite while(1) loop in the same thread as the GUI, because it will block the event loop.

    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QWidget w;
    w.show();

    // exec() runs the event loop which drives the GUI. It will not return until you stop the event loop.
    int i = a.exec();
    
    // Your main() function can't return until you stop your event loop.
    return i;
    

    }
    @

    [quote author="xmaze" date="1422978617"]i have a segmentation error in this line @if((data = ((unsigned char *) malloc(data_len)))== NULL) exit(1);@
    I know that malloc() it is not good in C++, but why in Constructor the program runs and if i use a push button the program corrupts?[/quote]Run a debugger. Is the segmentation fault caused by malloc() or exit()?

    Anyway, even if you don't get a segmentation fault, your GUI will stop responding when you start the infinite loop in mainloopC().



  • [quote author="JKSH" date="1422980371"]Hi,

    Run a debugger. Is the segmentation fault caused by malloc() or exit()?

    Anyway, even if you don't get a segmentation fault, your GUI will stop responding when you start the infinite loop in mainloopC().[/quote]

    The malloc has the problem, i tried also the following : @data=new char[data_len]; @ but it also returns a segmentation fault.
    Why in constructor works and the GUI also don't return a segmentation fault?


  • Moderators

    [quote author="xmaze" date="1422981305"]Why in constructor works and the GUI also don't return a segmentation fault? [/quote]I don't know. What is the value of data_len?



  • [quote author="JKSH" date="1422981695"][quote author="xmaze" date="1422981305"]Why in constructor works and the GUI also don't return a segmentation fault? [/quote]I don't know. What is the value of data_len?[/quote]

    262620



  • There isn't enough information to suggest anything useful.

    Since the original program had this in the main() function I assume that 'data' is global and is used globally by other modules. Moving this to a point further in the program means that 'data' might be created too late. The original program might have required this to be created and properly initialized before anything else (?).

    If you create another variable and use malloc() does the program still crash on malloc()? If it does maybe malloc() has been re-defined or overloaded somehow.



  • I found what is wrong but i dont know why!!

    If i try to allocate the memory with malloc in constructor everything its ok.
    if i try to allocate memory with malloc in an other section of the program i receive NULL pointer and segmentation fault.



  • It is hard to tell what is wrong without a code.
    Could you post simplified example of not-working code.



  • !http://i58.tinypic.com/s1pzdk.jpg!


    !http://i62.tinypic.com/1053821.jpg!

    This is the screen shots, the code is this one
    @data = ((unsigned char *) malloc(data_len);@

    If i put this line on Constructor i have the above results, it is successful,
    if i put the code for example in a function like On_pushButton_clicked,
    then i have the results in the second scree shot with a null pointer.



  • Here is a simple example that works.
    Could you try it and tell if it works in your environment.

    @
    #include <stdlib.h>
    #include <iostream>

    class Foo
    {
    public:
    Foo()
    {
    data_len = 262620;
    data = (unsigned char )malloc(data_len);
    }
    ~Foo()
    {
    if (data) {
    free((void
    )data);
    }
    }

        unsigned char* getData() { return data; }
        u_int32_t getDataLen() { return data_len; }
    
    private:
        unsigned char* data;
        u_int32_t data_len;
    

    };

    int main(int, char **)
    {
    Foo a_foo;

    std::cout << "Foo: data = " << std::hex << static_cast<void*>(a_foo.getData())
              << " data_len = " << std::dec << a_foo.getDataLen() << std::endl;
    
    unsigned char* data;
    u_int32_t data_len;
    data_len = 262620;
    data = (unsigned char *)malloc(data_len);
    std::cout << "Local: data = " << std::hex << static_cast<void*>(a_foo.getData())
              << " data_len = " << std::dec << a_foo.getDataLen() << std::endl;
    if (data) {
        free((void*)data);
    }
    
    return 0;
    

    }
    @



  • yes its working



  • It means that the problem is not in the location of this particular line but something else.



  • ok, i worked around and i think it's ok.

    Do you have a suggestion how can i use the while(1) {} with the GUI ?
    without corruption ?

    if i use in while() a function like addWidgetItem in a TreeWidget then will be ok ?



  • If you have a code that requires to run its own loop then I suggest to put it into a separate thread and run the code using QThread::run().
    Take a look on this "article":http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/ for an example.



  • Hi, not that this would fix your problem, however, the most common mistake I've ever seen in ANSI C coding is uninitialized pointers before use. Make it a good habit to always delcare, then initialize, and then use. I use malloc all over the place in QT without problems, however, I always initalize it in the constructor. Example in your Foo constructor place data = NULL; also do not forget to test and free then in your destructor or you may get memory leakage, example if(data != NULL) free(data); (which I see you have done). May not be your problem but it is good practice.


  • Moderators

    [quote author="xmaze" date="1423001212"]if i use in while() a function like addWidgetItem in a TreeWidget then will be ok ?[/quote]Your GUI will freeze until your while-loop exits.

    So, if the loop runs for a very short time only, then it's ok. If the loop runs for a long time, then your GUI will stop responding.



  • [quote author="JKSH" date="1423013834"][quote author="xmaze" date="1423001212"]if i use in while() a function like addWidgetItem in a TreeWidget then will be ok ?[/quote]Your GUI will freeze until your while-loop exits.

    So, if the loop runs for a very short time only, then it's ok. If the loop runs for a long time, then your GUI will stop responding.[/quote]

    QThread is it better or can i use also the pthread libs ?


  • Moderators

    QThread is more portable.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.