pdwyer wrote:I don't understand how you can say "in this case"
[...] this is going to be a leak isn't it?
Could you explain a little more the logic of leaving it off
No this is not a memory leak. Of course, I want to share my PB-knowledge with you, so I explain it to you.
If you call the procedure for the
first time, then the
If checks the value of the variable 'location'. location is 0, so the
If is true. Now, inside the
If, the variable location is filled with a number. This number is the ID of the RefularExpression.
The point is, that 'location' is
Static and not
Protected. You could also use a
Global variable instead. The advantage of the static variable is, that it even
keeps it value when the procedure is over.
So, what happens, when you call the procedure for the second time? When you call the procedure, the
If at the beginning checks, if 'location' has got a value. Yes, it has (from the last time you called the procedure). But the
If does not trigger, because there is a
Not. In fact, you could also write
If location <> 0 which is the same.
So the
If does only fire once - consequence: The expression is only created once. That's why there is no memory leak. This method offers 2 advantages:
1. You don't have to create the regular expression every time; this saves time.
2. You don't have to free it at the end of your procedure. Why? Because PureBasic automatically frees everything at program exit, as Fred said.
@ Trond
> post a code with a resource leak and claim it's "better"
I don't say my code is better, because it "offers" a leak. I claim the "better-status", because it's faster (less code), simpler (recursion instead of loop) and more comfortable (integrated depth-param instead of a constant).
> Let's say you spawn 10000 threads
The limit is 2.000

Furthemore, if you want to create 2.000 threads, then I ask you: Why? Creating 2.000 threads - THAT is bad coding.
[sentence removed]
If you want to use so many threads, please feel free to use this code instead:
Code: Select all
Procedure.s traceURL(URL$, RecursionDepth=#INFINITE)
If RecursionDepth
Protected location.l=CreateRegularExpression(#PB_Any, "(?im)^Location: .*$")
Protected Dim subPattern$(0)
If ExtractRegularExpression(location, GetHTTPHeader(URL$), subPattern$())
URL$=traceURL(Mid(subPattern$(0), 11), RecursionDepth-1)
EndIf
FreeRegularExpression(location)
EndIf
ProcedureReturn URL$
EndProcedure