Page 1 of 3

Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 12:25 am
by Christian Uceda

Code: Select all

EnableExplicit

Procedure test()
  Define str_keypressed.s, lng_operationresult.i
  PrintN("Test, press esc key or the n key")
  Repeat
    str_keypressed.s = Inkey()    
    Delay(20)
  Until str_keypressed <> ""

  Select RawKey()
    Case 27, 78 ; esc and n key respectively
      PrintN("Info! Operation aborted buy the user.")
      lng_operationresult = #False
      Goto Finish
  EndSelect
  
  lng_operationresult = #True
  Finish:
  ProcedureReturn lng_operationresult
EndProcedure

If OpenConsole()
  PrintN(Str(Test()))
  CloseConsole()
EndIf
The code crashes both compiled or on the ide in versions 4.41 and 4.50 when exiting the test procedure.

The IDE catches the crash as "invalid memory access" in 4.41 and "illegal instruction (executing binary data?)" on 4.50

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 12:48 am
by netmaestro
You cannot use GoTo to exit a Select block. It corrupts the stack. Also, please start queries like this in Coding Questions.

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 12:50 am
by mk-soft
"Goto" a bad method to leave "Select… EndSelect. The use of "Break" is better. "Goto" caused into this case a Stackcorruption.

P.S Sorry, no break. It´s verfy late...

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 12:52 am
by rsts
I get the crash. I don't think it like your goto from within the select.

cheers

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 12:57 am
by Christian Uceda
Hi, thanks for the quick reply.

I will avoid using select in this specific situation.

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 12:58 am
by netmaestro
Try this:

Code: Select all

EnableExplicit

Procedure test()
  Define str_keypressed.s, lng_operationresult.i
  PrintN("Test, press esc key or the n key")
  Repeat
    str_keypressed.s = Inkey()    
    Delay(20)
  Until str_keypressed <> ""
  
  Select RawKey()
    Case 27, 78 ; esc and n key respectively
      PrintN("Info! Operation aborted buy the user.")
      lng_operationresult = #False
    Default 
      lng_operationresult = #True
  EndSelect
  
  ProcedureReturn lng_operationresult
EndProcedure

If OpenConsole()
  PrintN(Str(Test()))
  Print("Press any key to exit...")
  Input()
  CloseConsole()
EndIf
And welcome to the forums! :mrgreen:

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 1:03 am
by Christian Uceda
Hi,

Thanks for the welcome! :D

I'm certainly new here :D

Do not worry about the abobe code, it is just an example I made, please do not try to fix it, it is just illustrative of the crash, not the actual code on the program.

I appreciate your efforts anyway :D

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 1:04 am
by IdeasVacuum
Crashes v4.40x86 with "illegal instruction (executing binary data?)"

Only fails if the Goto is used, but you can use select:

Code: Select all

EnableExplicit

Procedure test()
  Define str_keypressed.s, lng_operationresult.i
  PrintN("Test, press esc key or the n key")
  Repeat
    str_keypressed.s = Inkey()   
    Delay(20)
  Until str_keypressed <> ""

  Select RawKey()
    Case 27, 78 ; esc and n key respectively
      PrintN("Info! Operation aborted by the user.")
      lng_operationresult = #False

    Default
    PrintN("Info! Operation not aborted")
    lng_operationresult = #True

  EndSelect
 
  ProcedureReturn lng_operationresult
EndProcedure

Define iGood.i = #True

If OpenConsole()
  PrintN(Str(Test()))
  Delay(2000)
EndIf

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 1:09 am
by Christian Uceda
Wow many thanks again!

You guys are overwhelmingly... great! (and fast) :D

I repeat, do not worry about the code, I'm using a different approach on the code now.

I Just wanted to report this in case the crash is something the developers needed to look at.

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 1:03 pm
by luis
That's why I was suggesting it to mention it in the manual

http://www.purebasic.fr/english/viewtop ... 04#p310704

because it's quite obvious you should not exit a procedure with a goto, but about the select/endselect it's not obvious at all, since some compilers implements the select using gotos or jump tables but without using the stack.

Would be nice to have clearly stated if there are other constructs who deserve special attention with gotos.

offtopic: would be also nice to know which native commands/functions are replaced with inline code by the compiler.

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 10:27 pm
by netmaestro
Would be nice to have clearly stated if there are other constructs who deserve special attention with gotos.
Basically, Goto commands should be avoided in any file containing Purebasic sourcecode. It should be OK in a note to yourself, e.g. "goto store for milk".

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 10:47 pm
by luis
I disagree, in a language without exceptions handling the GOTO is the only way to keep your sanity and to exit from deep-nested nice structured code when something bad happens without using disguised gotos like breaks or god-forbid "break n" or dubious flags to be checked at every level.

In those cases I find GOTOs to a common exit point in the proc where the cause of the error can then be analyzed and acted upon a lot easier to understand and maintain too.

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 10:58 pm
by netmaestro
That's what the OnError lib is for :mrgreen:

Re: Can anyone verify this crash too?

Posted: Sun Aug 01, 2010 11:32 pm
by luis
In theory, but I don't know if you ever tried to use it...

I did and found too much problems with that. Even if you look at the manual you'll see it's better suited to trap fatal errors to be followed by the the program termination.

Not to mention sometime there are interactions with the debugger.

I found very unreliable trying to generate a custom error with RaiseError(), trapping it with OnErrorGoto() and actually trying to continue from here.

I prefer to avoid it.

That's exactly what I do without problems and without linking more code with a simple goto... or variation on the theme -> http://www.purebasic.fr/english/viewtop ... =3&t=42555

Re: Can anyone verify this crash too?

Posted: Tue Aug 03, 2010 9:50 am
by blueznl
luis wrote:I disagree, in a language without exceptions handling the GOTO is the only way to keep your sanity and to exit from deep-nested nice structured code when something bad happens without using disguised gotos like breaks or god-forbid "break n" or dubious flags to be checked at every level.

In those cases I find GOTOs to a common exit point in the proc where the cause of the error can then be analyzed and acted upon a lot easier to understand and maintain too.
Luis, now I disagree :-)

You see, Goto is evil :-)

Seriously, there's little need for Goto. If things are many levels deep, why not use an 'exit flag'? Something like...

Code: Select all

allisfine = #true
...
...
If ...
  If allisfine And ...
    While allisfine And ...
      ...
    Wend
  Endif
Endif
...
Thus far, I've never ever felt the need for Goto.