Yay!! more random crashing!

BlitzMax Forums/BlitzMax Programming/Yay!! more random crashing!

This is directly related to my other questions about using Google to translate words.
In this case I made a program that displays the whole Greek alphabet, and translates words ENG>GRC and GRC>ENG (Grescaro).

Anywho, when you try to translate a word or phrase a number of times (never seen any greater or lower than 18 on Windows, and it varies on Linux), no matter which direction (eng>grc or grc>eng) it will crash at COMPLETELY RANDOM places.

The test code is stripped out of Grescaro's, the last test I did on Linux gave me 12 tries, and it crashed at line 50:
Local url:String = "ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + word + "&langpair=" + langpair.Replace("|", "%7C")

with "Unhandled Exception:appstub.linux signal handler 11".

The code:
SuperStrict

Framework bah.libcurl

Import wx.wxApp 'Why is this needed? if I don't import wx.wxApp it crashes with a series of errors.
Import wx.wxjson

Import brl.retro
Import pub.stdc

Local translation:String, word:String = "Hello", langpair:String = "en|el"
For Local i:Int = 1 To 18
  translation = TranslationInterface.TranslateWord(word, langpair)
	
	DebugLog "Try #" + i + ", English to Greek: " + word + " to.. " + translation
	Delay 3000 'Don't flood Google!
Next

End


'Test wrapper for Translation (I beleive the crash [after precisely 18 {more or less on linux} tries at a translation] is caused by creating so many JSON objects,
'so lets try and make them single instance, just updating them everytime a phrase gets translated)....... nope, still crashing!
Type TranslationInterface
  Global jreader:wxJSONReader = New wxJSONReader.Create(), root:wxJSONValue
	
		'THANKS BRUCEY
		Function TranslateWord:String(word:String, langpair:String = "en|el")
		  Local curl:TCurlEasy = TCurlEasy.Create(), res:Int, ltype:Int = 0
		  Local data:String
			
			'As long as the source language is English it should already be good to go, otherwise
			'we should convert it to UTF-8 (it works fine in our case, as we only have Greek>English and English>Greek)
			If Left(langpair, Instr(langpair, "|") - 1).ToLower() <> "en"
				word = _textConvertMaxToUTF8(word)
				word = curl.escape(word)
				word = word.Replace(" ", "+")
				
			   ltype = 1
			   
			Else
				word = word.Replace(" ", "+")
				
			EndIf
		  
		  	curl.setOptInt(CURLOPT_FOLLOWLOCATION, 1)
			curl.setWriteString()
			
		  Local url:String = "ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + word + "&langpair=" + langpair.Replace("|", "%7C")
			curl.setOptString(CURLOPT_URL, url)
			curl.setOptString(CURLOPT_REFERER, "http://hai2you2.googlepages.com/")
			
			res = curl.perform()
			curl.CleanUp()
			
				If res = 0
					
					data = curl.ToString()
					
				Else
					
					DebugLog "TranslateWord(~q" + url + "~q); ERROR: failed to obtain data (code: " + res + ")"
					
					data = Null
					
				End If
				
			If res = 0
			  'Local jreader:wxJSONReader = New wxJSONReader.Create(), root:wxJSONValue = jreader.ParseString(data)
			  root = jreader.ParseString(data)
			
				If root <> Null
				  Local respstatus:Int = root.Item("responseStatus").AsInt()
				  
					If respstatus = 200
					  Local trtext:String = root.Item("responseData").Item("translatedText").AsString()
						
						If ltype = 0
							
							trtext = _xmlConvertUTF8ToMax(trtext)
							
						End If
						
					   Return trtext
					   
					Else
						
						DebugLog "Received data, but it is invalid (code: " + respstatus + " )"
						
					End If
				EndIf
				
			Else
				
				DebugLog "Failed to send translation data to Google (code: " + res + ")"
				
			End If
			
		   Return Null
		   
	End Function
	
End Type


'Conversion functions taken from bah.libxml
Function _textConvertMaxToUTF8:String(Text:String)
	If Not text Then
		Return ""
	End If
	
	Local l:Int = text.length
	If l = 0 Then
		Return ""
	End If
	
	Local count:Int = 0
	Local s:Byte[] = New Byte[l * 3] ' max possible is 3 x original size.
	
	For Local i:Int = 0 Until l
		Local char:Int = text[i]

		If char < 128 Then
			s[count] = char
			count:+ 1
			Continue
		Else If char<2048
			s[count] = char/64 | 192
			count:+ 1
			s[count] = char Mod 64 | 128
			count:+ 1
			Continue
		Else
			s[count] =  char/4096 | 224
			count:+ 1
			s[count] = char/64 Mod 64 | 128
			count:+ 1
			s[count] = char Mod 64 | 128
			count:+ 1
			Continue
		EndIf
		
	Next

	Return String.fromBytes(s, count)
End Function

' converts a UTF character array from byte-size characters to short-size characters
' based on the TextStream UTF code...
Function _xmlConvertUTF8ToMax:String(s:Byte Ptr)
	If s Then
		Local l:Int = _strlen(s)
		Local b:Short[] = New Short[l]
		Local bc:Int = -1
		Local c:Int
		Local d:Int
		Local e:Int
		For Local i:Int = 0 Until l
			bc:+1
			c = s[i]
			If c<128 
				b[bc] = c
				Continue
			End If
			i:+1
			d=s[i]
			If c<224 
				b[bc] = (c-192)*64+(d-128)
				Continue
			End If
			i:+1
			e = s[i]
			If c < 240 
				b[bc] = (c-224)*4096+(d-128)*64+(e-128)
				Continue
			End If
		Next
	
		Return String.fromshorts(b, bc + 1)
	End If
	
	Return ""
	
End Function

' _strlen needed for the _xmlConvertUTF8ToMax() function
Extern
	Function _strlen:Int(s:Byte Ptr) = "strlen"
	
End Extern


I originally didnt use everything against a single JSONReader instance, at first I was just creating one each time the user tried to translate a word.

NOTE: The Greek translation was formatted for use on a wxFrame, so it probably wont look correct in your console.

OK, I was able to translate a phrase upto 20 times in Grescaro, but after all the tests (using the code I posted) I can only get to 12 tries, and it's only crashing on line 50... (LINUX - Ubuntu 8.04)

Good grief, you've been ripping code out of everywhere :-p

I got to 12 here :-)

Good grief, you've been ripping code out of everywhere :-p
:D

In Grescaro it was crashing on all sorts of areas, like creating the JSONReader/parsing the string, and performing a Left operation..

Holds up hand in shame...

My glue was broken... turns out it was crashing as soon as GC kicked in... all this C++ stack/heap/pointer stuff breaks my head.

Get an update from SVN, build mods, and see how it goes now.


Sorry.

all this C++ stack/heap/pointer stuff breaks my head.
I can't even look at C++ code without asking "who's the fellow who scribbled this nonsense!?"

Get an update from SVN, build mods, and see how it goes now.
Coolio, working on it..

Anyhoo... shouldn't you be working on the Form Designer? I was hanging out for that for another project of mine ;-)

Works! Thanks again..

Anyhoo... shouldn't you be working on the Form Designer? I was hanging out for that for another project of mine ;-)
Keke, right away! - what might this project be?

what might this project be?

Just a personal one for me and some friends... given that I've ripped audio and logos from goodness knows where, it can't really be anything but :-)

But it does let me bring together lots of different modules, and play around with things I might not normally delve into - like a flash-like timeline thing for feeding together moving text and images to a soundtrack...
...and messing around with MiniB3D for nice visual effects.

Well, you gotta have your fun somewhere :-)

Well, you gotta have your fun somewhere :-)
Indeed!