Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-08

TTreeView, string and SIGSEGV

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Xiangrong Fang

Hi All,

I encountered a strange SIGSEGV with the attached program. To reproduce the problem:

1) compile and run it.
2) select "Node #2"
3) click "Test" button, it will insert a node before the selected Node
4) close the program

There will be a SIGSEGV at the end of OnDestroy method.

This problem is reproducible under Linux amd64 and Wine and Win7 (32bit). A strange thing is that on Linux or Wine, this error only occur on Node #1 and Node #2, but on Windows, it only occur on Node #2 and Node #3.

Also, if you swap either of the 2 commented statements, this error is gone.

I suspect this is caused by the AnsiString manager. Could anyone explain why, or is this a bug?

Thanks a lot.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130823/5a5d91dd/attachment.html> -------------- next part --------------
A non-text attachment was scrubbed...
Name: tree.tar.gz
Type: application/x-gzip
Size: 2031 bytes
Desc: not available
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130823/5a5d91dd/attachment.bin>

Reply by email

Martin
to Xiangrong Fang
Xiangrong Fang wrote 22 lines
Hi All, I encountered a strange SIGSEGV with the attached program. To reproduce the problem: 1) compile and run it. 2) select "Node #2" 3) click "Test" button, it will insert a node before the selected Node 4) close the program There will be a SIGSEGV at the end of OnDestroy method. This problem is reproducible under Linux amd64 and Wine and Win7 (32bit). A strange thing is that on Linux or Wine, this error only occur on Node #1 and Node #2, but on Windows, it only occur on Node #2 and Node #3. Also, if you swap either of the 2 commented statements, this error is gone. I suspect this is caused by the AnsiString manager. Could anyone explain why, or is this a bug?

Only had a quick look, please veryfy....

  Move(tv.Selected.Data^, p^, SizeOf(TNode));
   tv.Items.InsertObject(tv.Selected, 'New Node', p);

Move makes a copy of the node, including a copy of the ansistring (that is the pointer-part) in the node.
so now you have 2 strings, referring the same string data, but you never increased the ref count.

Reply by email

Xiangrong Fang
to Martin

Ok, that's reasonable, however:

1) If I use Move() is there a "manual" way to increate the string ref count?

2) why the error is gone if I change this line:

    prev := tv.Items.InsertObjectBehind(prev, p^.title, p);

to this:

    prev := tv.Items.InsertObjectBehind(prev, 'Node #' + IntToStr(i), p);

3) Why this error does not happen on node #0 and #3, but only node #1 and #2?

4) Why it behaves different on Windows and Linux?

Thanks!

2013/8/24 Martin

Show quoted text 45 lines
On 23/08/2013 16:13, Xiangrong Fang wrote: > Hi All, > > I encountered a strange SIGSEGV with the attached program. To reproduce > the problem: > > 1) compile and run it. > 2) select "Node #2" > 3) click "Test" button, it will insert a node before the selected Node > 4) close the program > > There will be a SIGSEGV at the end of OnDestroy method. > > This problem is reproducible under Linux amd64 and Wine and Win7 (32bit). > A strange thing is that on Linux or Wine, this error only occur on Node #1 > and Node #2, but on Windows, it only occur on Node #2 and Node #3. > > Also, if you swap either of the 2 commented statements, this error is > gone. > > I suspect this is caused by the AnsiString manager. Could anyone explain > why, or is this a bug? > Only had a quick look, please veryfy.... Move(tv.Selected.Data^, p^, SizeOf(TNode)); tv.Items.InsertObject(tv.**Selected, 'New Node', p); Move makes a copy of the node, including a copy of the ansistring (that is the pointer-part) in the node. so now you have 2 strings, referring the same string data, but you never increased the ref count. -- ______________________________**_________________ Lazarus mailing list .**freepascal.org http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130826/6a699304/attachment.html>

Reply by email

Mattias Gaertner
to Xiangrong Fang
Ok, that's reasonable, however: 1) If I use Move() is there a "manual" way to increate the string ref count?
Yes, but it is better to let the compiler do the work:
p^:=PNode(tv.Selected.Data)^;
Show quoted text 12 lines
2) why the error is gone if I change this line: prev := tv.Items.InsertObjectBehind(prev, p^.title, p); to this: prev := tv.Items.InsertObjectBehind(prev, 'Node #' + IntToStr(i), p); 3) Why this error does not happen on node #0 and #3, but only node #1 and #2? 4) Why it behaves different on Windows and Linux?

All this has the same answer:
When you mess up the heap you get results depending on the current memory, which depends on lots of stuff. The results can vary from host to host and might not even be the same on each run. In other words: random.
Windows and Linux have different approaches how to initialize memory, so you see differences more likely.

Mattias

Reply by email