I have a problem to translate a C program to QT GUI.
-
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.
-
[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 ?
-
QThread is more portable.