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. I have a problem to translate a C program to QT GUI.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
18 Posts 5 Posters 3.7k Views 1 Watching
  • 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.
  • X Offline
    X Offline
    xmaze
    wrote on last edited by
    #7

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #8

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

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xmaze
        wrote on last edited by
        #9

        !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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #10

          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;
          

          }
          @

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xmaze
            wrote on last edited by
            #11

            yes its working

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #12

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

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xmaze
                wrote on last edited by
                #13

                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 ?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andreyc
                  wrote on last edited by
                  #14

                  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.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Chrisw01
                    wrote on last edited by
                    #15

                    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.

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #16

                      [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.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • X Offline
                        X Offline
                        xmaze
                        wrote on last edited by
                        #17

                        [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 ?

                        1 Reply Last reply
                        0
                        • JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by
                          #18

                          QThread is more portable.

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          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