Recent

Author Topic: SetLength(Array) error?  (Read 5063 times)

typo

  • Hero Member
  • *****
  • Posts: 3051
SetLength(Array) error?
« on: October 21, 2014, 03:03:00 pm »
I have a SIGSEGV error and the line which may causing the error has only:

Code: [Select]
SetLength(FDynamicArray, Length(FDynamicArray) + 1);

This is an array of records, declared as follows:

Code: [Select]
TMyRecord = record
    Character :string[6];
    CharBold,
    CharItalic,
    CharUnderline,
    CharStrikeOut :boolean;
    CharColor,
    CharBackColor,
    CharPointX,
    CharPointY :integer;
  end;               

{...}

FDynamicArray :array of TMyRecord;         

The error occurs when the length is 267.

Any idea?

Thanks.
« Last Edit: October 22, 2014, 03:32:31 am by typo »

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: SetLength(Array) error?
« Reply #1 on: October 21, 2014, 03:15:39 pm »
Perhaps 'Length()'is the sum of the size of each item-type, of record? Try ShowMessage(IntToStr(Length(array)));
« Last Edit: October 21, 2014, 03:17:58 pm by exdatis »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: SetLength(Array) error?
« Reply #2 on: October 21, 2014, 03:18:29 pm »
This ShowMessage put just before the error shows 267.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: SetLength(Array) error?
« Reply #3 on: October 21, 2014, 03:23:22 pm »
I think you need to show more code.
Non-obvious SIGSEGV dynamic-array-related errors often have to do with overall code design and unconsidered assumptions about the continuous validity of the array indices used (where errors may not show up immediately), rather than just one apparently problematic line of code.

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: SetLength(Array) error?
« Reply #4 on: October 21, 2014, 03:53:17 pm »
i had a similar problem with a dynamic array used inside a paint procedure of a class
 i just removed all the related code oute side of the paint and all worked as excpected
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: SetLength(Array) error?
« Reply #5 on: October 21, 2014, 06:34:31 pm »
The error occurs when the length is 267.

Any idea?

i had a similar problem with a dynamic array used inside a paint procedure of a class
 i just removed all the related code oute side of the paint and all worked as excpected

I believe in both cases the dynamic array header is getting corrupted by something else. Moving the array to a different location will hide the symptoms, but will not cure the illness.

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: SetLength(Array) error?
« Reply #6 on: October 21, 2014, 07:22:16 pm »
i do not think there is an error other than logical one
Quote
I believe in both cases the dynamic array header is getting corrupted by something else.

setlenght does create a copy on dynamic arrays
« Last Edit: October 21, 2014, 07:25:41 pm by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: SetLength(Array) error?
« Reply #7 on: October 21, 2014, 07:48:03 pm »
i do not think there is an error other than logical one
Do you mean that typo is changing DynamicArray and using FDynamicArray instead?

setlenght does create a copy on dynamic arrays
It does more than that. There is a little header in the minus offset. Check fpc_dynarray_setlength in rtl\inc\dynarr.inc. Plus, he might be overwriting the pointer on the stack instead.

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: SetLength(Array) error?
« Reply #8 on: October 21, 2014, 08:13:44 pm »
Quote
Do you mean that typo is changing DynamicArray and using FDynamicArray instead?
yes or a temp copy
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

 

TinyPortal © 2005-2018