Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, April 2008
Problem with QString


Message 1 in thread

Hi,

My program got the segmentation fault error at the function 
QString::Latin1_helper() in libQtCore.so.4. The error does not occur always 
but time to time. It seems to me that some problem happened when Qt tried to 
convert a char* to qstring, but I have no idea why.

Do you expect any reason for that error? Please give me your idea. I would 
much appreciate. Thank you!

Regards,

.Viet Trung.

--
 [ signature omitted ] 

Message 2 in thread

Do Viet Trung wrote:
>Hi,
>
>My program got the segmentation fault error at the function
>QString::Latin1_helper() in libQtCore.so.4. The error does not occur
> always but time to time. It seems to me that some problem happened when
> Qt tried to convert a char* to qstring, but I have no idea why.
>
>Do you expect any reason for that error? Please give me your idea. I
> would much appreciate. Thank you!

Unfortunately, without more information it's impossible for us to tell you 
what the problem is.

Can you try the following:
1) run your program inside valgrind and see if it reports any memory 
errors

2) run your program inside a debugger and, when it does crash, send us the 
backtrace

3) come up with a small and compilable testcase which shows the problem, 
then post it to the list

4) failing all of the above, post to the list an excerpt of your code 
showing where the bug appears.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

Hi,

Thanks for your suggestions.

I have tried what you suggest. And here is some more information:

1) Inside valgrind, I got following memory error:
//--------------------------------------------
==5737== Invalid read of size 1
==5737==    at 0x4091105: QString::fromLatin1_helper(char const*, int) 
(in /usr/lib/libQtCore.so.4.3.2)
==5737==    by 0x40993C0: QString::fromAscii_helper(char const*, int) 
(in /usr/lib/libQtCore.so.4.3.2)
==5737==    by 0x80625E5: CQtLink::CQtLink(char*, CConnection*, void*) 
(qstring.h:371)
==5737==    by 0x806B2DF: CQtViewFactory::CreateView(char*, CObservable*, 
void*) (CQtViewFactory.cpp:31)
==5737==    by 0x8094D18: CGraphicalNode::GenerateInternalStructure() 
(CGraphicalNode.cpp:639)
==5737==    by 0x805EC0C: CQtGraphViewer::DrawGraph(CNetworkModule*) 
(CQtGraphViewer.cpp:120)
==5737==    by 0x80B56CC: CQtGraphViewer::qt_metacall(QMetaObject::Call, int, 
void**) (moc_CQtGraphViewer.cpp:69)
==5737==    by 0x410D6EA: QMetaCallEvent::placeMetaCall(QObject*) 
(in /usr/lib/libQtCore.so.4.3.2)
==5737==    by 0x410F7D8: QObject::event(QEvent*) 
(in /usr/lib/libQtCore.so.4.3.2)
==5737==    by 0x5566891: QWidget::event(QEvent*) 
(in /usr/lib/libQtGui.so.4.3.2)
==5737==    by 0x5523A3F: QApplicationPrivate::notify_helper(QObject*, 
QEvent*) (in /usr/lib/libQtGui.so.4.3.2)
==5737==    by 0x5529A7D: QApplication::notify(QObject*, QEvent*) 
(in /usr/lib/libQtGui.so.4.3.2)
==5737==  Address 0x6D0061 is not stack'd, malloc'd or (recently) free'd
==5737==
==5737== Process terminating with default action of signal 11 (SIGSEGV)
==5737==  Access not within mapped region at address 0x6D0061
==5737==    at 0x4091105: QString::fromLatin1_helper(char const*, int) 
(in /usr/lib/libQtCore.so.4.3.2)
==5737==    by 0x40993C0: QString::fromAscii_helper(char const*, int) 
(in /usr/lib/libQtCore.so.4.3.2)
==5737==    by 0x80625E5: CQtLink::CQtLink(char*, CConnection*, void*) 
(qstring.h:371)
==5737==    by 0x806B2DF: CQtViewFactory::CreateView(char*, CObservable*, 
void*) (CQtViewFactory.cpp:31)
==5737==    by 0x8094D18: CGraphicalNode::GenerateInternalStructure() 
(CGraphicalNode.cpp:639)
==5737==    by 0x805EC0C: CQtGraphViewer::DrawGraph(CNetworkModule*) 
(CQtGraphViewer.cpp:120)
==5737==    by 0x80B56CC: CQtGraphViewer::qt_metacall(QMetaObject::Call, int, 
void**) (moc_CQtGraphViewer.cpp:69)
==5737==    by 0x410D6EA: QMetaCallEvent::placeMetaCall(QObject*) 
(in /usr/lib/libQtCore.so.4.3.2)
==5737==    by 0x410F7D8: QObject::event(QEvent*) 
(in /usr/lib/libQtCore.so.4.3.2)
==5737==    by 0x5566891: QWidget::event(QEvent*) 
(in /usr/lib/libQtGui.so.4.3.2)
==5737==    by 0x5523A3F: QApplicationPrivate::notify_helper(QObject*, 
QEvent*) (in /usr/lib/libQtGui.so.4.3.2)
==5737==    by 0x5529A7D: QApplication::notify(QObject*, QEvent*) 
(in /usr/lib/libQtGui.so.4.3.2)
//--------------------------------------------

2) Using ddd(gdb), I got the backtrace for the error as follows:
//--------------------------------------------
...
#3 0x0806b2e0 in CQtViewFactory::CreateView() at CQtViewFactory.cpp: 31
#2 0x080625e6 in CQtLink() at qstring.h:371
#1 0x0017a3c1 in QString::fromAscii_helper() from libQtCore.so.4
#0 0x00172105 in QString::fromLatin1_helper() from libQtCore.so.4
//--------------------------------------------

The error occurs when my program tries to create an instance of class CQtLink
whose constructor is defined as:
//--------------------------------------------
CQtLink::CQtLink(char* objectId, CConnection* connection, void* container=0): 
QGraphicsLineItem::QGraphicsLineItem(0), 
CGraphicalLink::CGraphicalLink(objectId, connection, container);
//--------------------------------------------

As you can see, CQtLink is derived from QGraphicsLineItem and a none-Qt class 
CGraphicalLink.

I have already checked input arguments, including objectId, connection 
(CConnection is none-Qt class) and container.  None of them is invalid. 

Additionally, the error occurs before the program runs inside body code of 
CQtLink's constructor, and with something related to Qt. So I guess some 
problem happens when program calls the constructor of CGraphicsLineItem, 
something about the un-initialization of some variable, I think. But still I 
cannot figure out what it is.

I am looking forward to your replies. Thank you.

Regards,

.Viet Trung.



On Sunday 27 April 2008 16:32, Thiago Macieira wrote:
> Do Viet Trung wrote:
> >Hi,
> >
> >My program got the segmentation fault error at the function
> >QString::Latin1_helper() in libQtCore.so.4. The error does not occur
> > always but time to time. It seems to me that some problem happened when
> > Qt tried to convert a char* to qstring, but I have no idea why.
> >
> >Do you expect any reason for that error? Please give me your idea. I
> > would much appreciate. Thank you!
>
> Unfortunately, without more information it's impossible for us to tell you
> what the problem is.
>
> Can you try the following:
> 1) run your program inside valgrind and see if it reports any memory
> errors
>
> 2) run your program inside a debugger and, when it does crash, send us the
> backtrace
>
> 3) come up with a small and compilable testcase which shows the problem,
> then post it to the list
>
> 4) failing all of the above, post to the list an excerpt of your code
> showing where the bug appears.

--
 [ signature omitted ] 

Message 4 in thread

Do Viet Trung wrote:
>1) Inside valgrind, I got following memory error:
>//--------------------------------------------
>==5737== Invalid read of size 1
>==5737==    at 0x4091105: QString::fromLatin1_helper(char const*, int)
>(in /usr/lib/libQtCore.so.4.3.2)
>==5737==    by 0x40993C0: QString::fromAscii_helper(char const*, int)
>(in /usr/lib/libQtCore.so.4.3.2)
>==5737==    by 0x80625E5: CQtLink::CQtLink(char*, CConnection*, void*)
>(qstring.h:371)

You built your program in release mode, which means the line number above 
doesn't make much sense. In any case, as you yourself said:

>I have already checked input arguments, including objectId, connection
>(CConnection is none-Qt class) and container.  None of them is invalid.

the argument isn't valid. So the solution is simple: make it valid. :-)

Now, we don't know your code, so we can't say what exactly is not valid. I 
suggest rebuilding your code in debug mode so the line numbers shown by 
valgrind and gdb make more sense. Then find out which line, exactly, in 
CQtLink's constructor is the one with problems.

Then just make sure the argument it is passing is valid and initialised.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 5 in thread

Hi,

Glad to get your reply.

> You built your program in release mode, which means the line number above
> doesn't make much sense. 
I will try to compile it in debug mode. Thanks!

> In any case, as you yourself said: 
> >I have already checked input arguments, including objectId, connection
> >(CConnection is none-Qt class) and container.  None of them is invalid.
>
> the argument isn't valid. So the solution is simple: make it valid. :-)
--> Sorry but I had said, "none of them is invalid", which means all of them 
are assigned with valid values. Sorry for my unclear words.

Thank you very much for your time.

Best regards,

.Viet Trung.







On Sunday 27 April 2008 20:13, Thiago Macieira wrote:
> Do Viet Trung wrote:
> >1) Inside valgrind, I got following memory error:
> >//--------------------------------------------
> >==5737== Invalid read of size 1
> >==5737==    at 0x4091105: QString::fromLatin1_helper(char const*, int)
> >(in /usr/lib/libQtCore.so.4.3.2)
> >==5737==    by 0x40993C0: QString::fromAscii_helper(char const*, int)
> >(in /usr/lib/libQtCore.so.4.3.2)
> >==5737==    by 0x80625E5: CQtLink::CQtLink(char*, CConnection*, void*)
> >(qstring.h:371)
>
> You built your program in release mode, which means the line number above
>
> doesn't make much sense. In any case, as you yourself said:
> >I have already checked input arguments, including objectId, connection
> >(CConnection is none-Qt class) and container.  None of them is invalid.
>
> the argument isn't valid. So the solution is simple: make it valid. :-)
>
> Now, we don't know your code, so we can't say what exactly is not valid. I
> suggest rebuilding your code in debug mode so the line numbers shown by
> valgrind and gdb make more sense. Then find out which line, exactly, in
> CQtLink's constructor is the one with problems.
>
> Then just make sure the argument it is passing is valid and initialised.

--
 [ signature omitted ] 

Message 6 in thread

On Sunday 27 April 2008 16:06:35 Do Viet Trung wrote:

> The error does not occur always but time to time. 

check your cross-thread resources.


-- 
 [ signature omitted ] 

Message 7 in thread

Viet wrote:

> Hi,
> 
> My program got the segmentation fault error at the function 
> QString::Latin1_helper() in libQtCore.so.4. The error does 
> not occur always 
> but time to time. It seems to me that some problem happened 
> when Qt tried to 
> convert a char* to qstring, but I have no idea why.
> 
> Do you expect any reason for that error? Please give me your 
> idea. I would 
> much appreciate. Thank you!
> 
> Regards,
> 
> .Viet Trung.

Hi, 

I am not sure about unix linkers, but in Windows MSVS 2005 you cannot rely
on the sequence of initialisers being called in any sort of sensible
dependency order. 

This especially applies to using global const objects like QString from
another global object being initialised. 

The solution is to call the initialisation yourself from main, or not use
global objects. 

It doesn't always happen, because it depends on the linker actions. in
Delphi, a similar problem occurs when doing full or partial builds, but I
don't know if that applies to C++. 

Hope that helps, 

Tony Rietwyk

--
 [ signature omitted ]