Click here to Skip to main content
14,983,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!
I am using this BackTrack procedure that Fills a map.
VB
Private Sub Fill(ByVal i As Integer, ByVal j As Integer)
        If (i < 1) Or (j < 1) Or (i > FillM.Width) Or (j > FillM.Height) Then
            Exit Sub
        End If
        If FillM.Pixel(i, j) = 1 Then
            FillM.Pixel(i, j) = -1

            Fill(i, j - 1)
            Fill(i, j + 1)
            Fill(i - 1, j - 1)
            Fill(i - 1, j)
            Fill(i - 1, j + 1)
            Fill(i + 1, j)
            Fill(i + 1, j - 1)
            Fill(i + 1, j + 1)
        End If
    End Sub

When The Map has a few pixels that have value 1, It works correctly. but when the map has many pixels that has value 1, It cause an StackOverflowException. How Can I resolve this problem and why .Net Framework 3.5 throw this invalid Exception????

[Modified: just added the lang setting to the pre tag to colorize it]
Posted
Updated 24-Jun-10 6:56am
v2

A StackOverflowException means that you have too many nested levels (or that you have too many recursive calls.

It's a memory issue and I'm not sure the best way to fix it for you. I might just looking in 4 directions instead of all 8. You should still check all of them and it might help solve the problem...though it also might not.

Another thing to do would be to create a static counter and after you reach a certain level, stop searching further and search as much as you can recursively. Then at the end, just check over each cell once more until there are no more 1's.

@Your response:

I'm just the messenger. I didn't build .Net so I can't say how much memory it allocates for each stack and how much memory you're using. I can only tell you why you're getting that exception based on the MSDN documentataion. (Which by the way, a simple google search of the exception would have told you everyhing that I did).
   
v2
Comments
Mehdi Ghiasi 24-Jun-10 13:19pm
   
This Means I can do BackTrack in Free Pascal and can't do BackTrack with .Net Framework 4 ??????????????????????????????????????????????????????????????????????????
Here is an article that may help you with this, though the example is C#.


http://www.devarticles.com/c/a/Development-Cycles/The-Backtracking-Algorithm-Technique/2/[^]
   
v3
in easy words!
if .Net get you 1Mbyte (Just Example) of memory to store data and you want to store 1.1Mbyte this throw StackOverflowException like if you use array and you get IndexOutOfRangeException, easy?

what's or where's problem?
you've get us just your "BackTrack" method and on-one known what you do in FillM.Pixel

if you've an array byte[1000000000000...][1000000000000...] and you want fill it, =) this sock you by gentle "StackOverflowException"
try to verify your FillM.Pixel and also verify if you don't have forget something and your "BackTrack" can be ended for not trapped by :

-> Call Ref Fill
 -> Call Ref Fill
    -> Call Ref Fill
	....
		-> you don't have memory to Call Ref Fill and you get socked by "StackOverflowException"
   
Comments
Adrabi Abderrahim 24-Jun-10 18:35pm
   
:D sorry
sock = shock/ socked = shocked
on-one = no-one
I Have solved my problem using this code:
VB
Dim FillI, FillJ As Integer

 Private Sub Fill2(Optional ByVal i As Integer = -1, Optional ByVal j As Integer = -1)
     If (i = -1) And (j = -1) Then
         Fill2(FillI, FillJ)
     End If
     If (i < 1) Or (j < 1) Or (i > FillM.Width) Or (j > FillM.Height) Then
         Exit Sub
     End If
     If FillM.Pixel(i, j) = 1 Then
         FillM.Pixel(i, j) = -1

         Fill2(i, j - 1)
         Fill2(i, j + 1)
         Fill2(i - 1, j - 1)
         Fill2(i - 1, j)
         Fill2(i - 1, j + 1)
         Fill2(i + 1, j)
         Fill2(i + 1, j - 1)
         Fill2(i + 1, j + 1)
     End If
 End Sub

 Private Sub Fill(ByVal i As Integer, ByVal j As Integer)
     FillI = i
     FillJ = j
     Dim T As New Thread(AddressOf Fill2, 1024 * 1024 * 20) ' maxStackSize is in bytes
     T.Start()
     Do
         Application.DoEvents()
     Loop Until ThreadState.Running <> T.ThreadState
 End Sub


So, is there any idea for Directly pass parameters using Thread at Dim T As New Thread(AddressOf Fill2, 1024 * 1024 * 20)??
   
Comments
Mehdi Ghiasi 25-Jun-10 2:54am
   
This Thread can increase my stack area and I will not get StackOverFlowException Error.
Check out Microsoft's MSDM for what your looking for???
   
Comments
Kschuler 24-Jun-10 14:05pm
   
I think you mean MSDN.
Sandeep Mewara 24-Jun-10 14:37pm
   
Reason for my vote of 1
What MSDN? MSDN has everything!
Issue is with the code and it is posted here. Answer is of no use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900