Recent

Author Topic: TRY-EXCEPT doesnt work in fpc 3.0.0!!! [solved]  (Read 16542 times)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #15 on: January 31, 2017, 03:16:25 pm »
Im sry for the rude tone, but im really pissed of by this problem currently...
I understand that this kind of thing is frustrating. Others know this as well. Just don't try to redirect that frustration to those that are trying to help you (because quite frankly, that pisses off me).

FPC is trying to help you as much as possible, but that doesn't take away the fact that when using pointers you are more or less telling FPC to keep the hell away unless something really goes bad. You are still allowed to write into parts that are allocated.

The sigv and things you did leading up to that is an indication that something like (eventually) overwriting memory is indeed happening. In case code is overwritten then things can become pretty much unpredictable.

Please when you are a bit less frustrated, read the thread again and ask yourself if it would be too much to ask for an example code that does exactly as in your program and that still produces the error. And yes i realize what you said that it only happens around the n-th iteration. In such cases it is usually advisable to post the complete project so other can have a look and perhaps pinpoint what is going wrong.
« Last Edit: January 31, 2017, 03:21:51 pm by molly »

Magicus

  • New Member
  • *
  • Posts: 13
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #16 on: January 31, 2017, 03:19:56 pm »
How to run without debugging?

@molly:

But i am still wondering, why FPC doesnt allow to catch External: SIGSEGV, in some other languages, outofBound Exception is able to catch up, by the compiler, or is the problem in my case, that some different code/memoryblock has been overwritten by my pointer, so that the compiler is unable to catch this action BEFORE the memory is overwritten and for that it cant be catched??

Am i understanding you right??
« Last Edit: January 31, 2017, 03:24:03 pm by Magicus »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #17 on: January 31, 2017, 03:24:32 pm »
I am not sure (as rvk wrote) about the inability to catch external sigv's. i would have to take a look for that.

It is indeed possible that it is too late under certain conditions. As said, i would have to test that.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #18 on: January 31, 2017, 03:26:36 pm »
How to run without debugging?
Like I said... Run > Run without Debugging.
(choose the menu Run, then click on Run without Debugging)

Quote
But i am still wondering, why FPC doesnt allow to catch External: SIGSEGV, in some other languages, outofBound Exception is able to catch up, by the compiler, or is the problem in my case, that it is too late to catch it?? Am i understanding your point right??
FPC DOES catch the SIGSEGV. You'll see that if you run without debugging or outside the IDE.

But when you are using debugging in the IDE, it uses GDB. And GDB falls over the SIGSEGV.
That's why you see the word External in External: SIGSEGV.

And GDB doesn't let you skip that. Normally a SIGSEGV is given when you overwrite memory that isn't yours. The rest of the program can become unpredictable that it's usually not a good idea to continue but to solve the SIGSEGV another way.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #19 on: January 31, 2017, 03:28:32 pm »
Please calm down. We here to solve the issue not to argue.

@Magicus

If try-except doesn't work on FPC 3.0.0, it should already been reported on the bug report, because a lot of users are using it, including me. I'm sure to tell you it does work on FPC 3.0.0.

So, what's wrong. That's the thing we're trying to find out. It can be some misconfiguration in your project files, FPC or maybe in your code. That's why we asking you to provide more information about the code.

I understand you may have your reasons not to publicize the code, but at least can you please write a short example of fully working and compile-able code and attached it to this forum? We need it because, if the code works correctly in our computers, that means there perhaps something wrong in your FPC settings.

For your information, I ever thought try-except not working on my Lazarus. After some testings and readings, then I found that the default Lazarus settings to always notify exception even we already catch it.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #20 on: January 31, 2017, 03:33:37 pm »
@rvk: ah yes, that explains things with regards to gdb. thanks for clarifying that.

Magicus

  • New Member
  • *
  • Posts: 13
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #21 on: January 31, 2017, 03:54:52 pm »
ok, i will start from the very beginning^^

Im currently working, just to leran more about data-structes, on a dynamic array, with an OWN- way of implementing it, and i just wanted to try one idea, and this idea is: (look at the picture)

And the idea of the Write(index, value) function is:

The moment the array of the first TBLOCK is full, create another TBLOCK, within a new array, which has x-new items and both blocks are pointing to each other.

CODE:
procedure TDynamicArray.Write(index: TIndex; Value: integer);
var
  start: TDwordPtr;
  newBlock: TBlockPtr = nil;
begin
   try
     begin
       start := f_first_block^.Start;
       (start + index)^ := value;
       f_count += 1;
     end;

   except On Exception do
     begin       
       newBlock := GetBlock(1000); //1000 is just an example...
       LinkBetween(f_first_block, newBlock);
     end;
   end;
end;     
« Last Edit: January 31, 2017, 03:56:45 pm by Magicus »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #22 on: January 31, 2017, 04:01:44 pm »
The moment the array of the first TBLOCK is full, create another TBLOCK, within a new array, which has x-new items and both blocks are pointing to each other.
As said... your code should run fine without the debugger but it's really bad programming practice to rely on an exception to do what you want.

You should keep a record of how large your array is (with a normal array you could do that with Length(array)).
Then, in your loop, you should check if you run out of that space and create a new block when needed.

You shouldn't just try to write to memory outside your block and let an exception detect if it's allowed.

B.T.W. It would be easier to use dynamic arrays in Pascal but I guess you're trying something here yourself (i.e. learn more about data-structes).

So, per block you need to keep an extra variable with the length of the array (you used to reserve the memory).
« Last Edit: January 31, 2017, 04:05:40 pm by rvk »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #23 on: January 31, 2017, 05:20:57 pm »
Completely agree.

What happens if the exception is:
Code: Pascal  [Select][+][-]
  1.   Except on E:EOutOfMemory do
  2.   ....
  3.   else
  4.   .??? you think you will reach this....
  5.  

Bad code.

Listen, not argue.
« Last Edit: January 31, 2017, 05:23:39 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #24 on: January 31, 2017, 06:01:57 pm »
ok, i will start from the very beginning^^
Let me return the favour and show you how things could go amiss. Imagine variable OverWriteMe to be a counter or an index  :)
Code: Pascal  [Select][+][-]
  1. program overwrite;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. (*
  6.   Example of overwriting values using wrong pointermath and FPC which is
  7.   unable to 'catch' that (just like in any other programming language for that
  8.   matter, except perhaps some scripting languages).
  9. *)
  10.  
  11. uses
  12.   SysUtils;
  13.  
  14. type
  15.   PSomeBlock = ^TSomeblock;
  16.   TSomeBlock = record
  17.     next : PSomeBlock;
  18.     Arr  : ^LongWord;
  19.   end;
  20.  
  21. var
  22.   SomeArray   : array[0..100] of LongWord;
  23.   OverWriteMe : LongWord;
  24.   SomeBlock   : TSomeBlock;
  25.  
  26.   First       : PSomeBlock;
  27.   StartInt    : ^LongWord;
  28.   ValueToWrite: LongWord;
  29.   index       : integer;
  30. begin
  31.   ValueToWrite := $11223344;  
  32.   OverWriteMe  := $12345678;        // Initialize variable that get's overwriten
  33.   SomeArray[0] := $12345678;        // Some value
  34.  
  35.   SomeBlock.Next := nil;            // init
  36.   SomeBlock.Arr  := @SomeArray[0];  // Point to some integer inside array
  37.  
  38.   first := @SomeBlock;              // first points to first block
  39.  
  40.   WriteLn( 'ValueToWrite.Value        = $', HexStr(ValueToWrite,8) );
  41.   WriteLn( 'OverWriteMe.Value         = $', HexStr(OverWriteMe,8) );
  42.   WriteLn( 'OverWriteMe.Address       = $', HexStr(Addr(OverWriteMe)) );
  43.   WriteLn( 'first.Value (ptr)         = $', HexStr(First) );
  44.   WriteLn( 'SomeBlock.Arr.Value (ptr) = $', HexStr(SomeBlock.Arr) );
  45.  
  46.   try
  47.     StartInt := first^.Arr;
  48.     WriteLn( 'StartInt.Value (ptr)      = $', HexStr(StartInt) );
  49.     // Calc some mailformed index
  50.     index := Addr(OverWriteMe) - StartInt;
  51.     WriteLn( 'index.value               = #', index, ' (pointer math at work for you)');
  52.     WriteLn( 'SizeOf(LongWord)          = ', SizeOf(LongWord));
  53.  
  54.     WriteLn('OverWriteMe.Address - StartInt.Address = ', Addr(OverWriteMe) - Pointer(StartInt), ' bytes');
  55.     WriteLn('Want to attempt to write to address $', HexStr(StartInt + index));
  56.     // actually write to the address with wrongly calculated index
  57. //    PLongWord(StartInt + index)^ := ValueToWrite;
  58.     (StartInt + index)^ := ValueToWrite;
  59.     WriteLn('Done ! Now let''s show some magic');
  60.     // Display the content of SomeValue variable
  61.     Writeln('OverWriteMe.Value = $', HexStr(OverWriteMe,8));
  62.    
  63.     WriteLn('Seems that we are still alive ?');
  64.   except
  65.     on E: Exception do
  66.     begin
  67.       WriteLn('Oops exception occured  ');
  68.       ShowException(ExceptObject, ExceptAddr);
  69.     end;
  70.   end;
  71. end.
  72.  

Quote
And the idea of the Write(index, value) function is:

The problem is ... even showing your write routine that will not help because things only manifests itself there. There is something going wrong elsewhere, or the write function is making wrong assumption that rely on what is happening behind the scenes.

However, since the error seems to manifests itself in the write function, you can use that function to inform yourself on the blocks being in use, all the pointers (and if they are all pointing inside valid memory blocks etc) and everything else that might have gone wrong.

For instance, in your quick mock-up of the write function no any checking is done on validity of the pointers, not any checking if the counter is within bounds. The same is true for your index.

So in order to 'catch' what's going wrong, start with checking some values and if they are in valid range (if not, print out the values and try to trace back where they are originating from).

edit: typo corrected in source, we are indexing longwords, not pointers.
« Last Edit: January 31, 2017, 06:16:16 pm by molly »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #25 on: January 31, 2017, 06:08:08 pm »
Can someone be plz be as impolite , insulting, and  grumpy >:( as me on the subject? I feel incapable.... O:-)
molly, don't be surprised if your eloquent effort is dismissed by a brick wall.
« Last Edit: January 31, 2017, 06:12:15 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #26 on: January 31, 2017, 06:19:21 pm »
Thank you for the warning Thaddy. Yes, i am aware.

Trying to make TS understand the consequence of his/her code is all i can do atm. There is something to be said of wanting to investigate and do things manually, how else would someone be able to learn? I've learned a lot from my ill written and crappy code (still do for that matter)  :)

I'm trying to cut down on my grumpy moods (or at least to not express them publicly). Even though i feel that it is justified at times.
« Last Edit: January 31, 2017, 06:23:22 pm by molly »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #27 on: February 02, 2017, 11:41:34 am »
Could you also post some small code which uses this unit and generates the error?

B.T.W. you now have this
Code: Pascal  [Select][+][-]
  1.  if index <= f_capacity then
  2.   begin
  3.     (f_arr_start + index)^ := Value;
  4.     f_length += 1;
  5.     f_empty := False;
  6.   end
But is capacity is 100 and you have an index of 100 you still go beyond the memory you reserved.
You should check on index < f_capacity.

(assuming f_arr_start contains the first reserved byte)

Magicus

  • New Member
  • *
  • Posts: 13
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #28 on: February 02, 2017, 11:43:10 am »
And honestly,

@molly

Im realtive in early stages with pointers, for that, it might be good possible, that im doing fatal mistakes in my code and thanks for you(r) suggestions :)

But i dont understand, why im able to allocate, 100.000 Items but cant just access 26.xxx, or something in that space but NEVER 100.000 ?!

Magicus

  • New Member
  • *
  • Posts: 13
Re: TRY-EXCEPT doesnt work in fpc 3.0.0!!!
« Reply #29 on: February 02, 2017, 11:43:56 am »
Yes of course :=)


CODE:

list := TDynamicArray.Create(100000);  //1.Block --> 100000 Array-Elemente

  for i := 0 to 100000 do
  begin
    list.Write(i, i*i);
  end;

 

TinyPortal © 2005-2018