Mistakenly crashed garry's mod by calling self:func() inside of the func()

Hello everyone,

It was my previous understanding that Garry’s Mod had facilities built in that acted as a safety net when us coders do something stupid, like calling a function inside of its self with no base case. I had the misconception that I could call a SWEP’s base PrimaryAttack() function as such:

function SWEP:PrimaryAttack()
if !self.Owner:KeyDown(IN_USE) then
–Basically the parent function
return self:PrimaryAttack()

When I probably should have been using base in place of self. None the less, I changed levels and fired up the weapon, so to speak. Lo and behold, both my gmod client and server choked up and died, without so much as an error.

So, I have to ask: Did I just do something so blatantly obvious that there isn’t anything built in to catch it, or is the infinite loop detection functionality disabled?

It’s not disabled, it not working properly for that case AFAIK.

Shouldn’t that throw stack overflow error after a while though.

Every time I’ve mistakenly caused a stack overflow it’s happened almost instantly.

It will stack-overflow if you remove the “return”:
[LUA]function SWEP:PrimaryAttack()
if !self.Owner:KeyDown(IN_USE) then
–Basically the parent function

[editline]20th October 2013[/editline]

In case you want to know the reason:
The code above is a normal function call which has a new stack frame, so with infinite recursion that one will eventually cause a stack overflow.
Your code, on the other hand, has a tail call, which is handled differently and thus prevents a stack overflow:


Which wasn’t supposed to trip gmod’s loop detection?

Why does it matter if it trips GMod’s loop detection or not? Fix your code and move on. The issue here shouldn’t be GMod’s infinite loop detection, it should be about your code.

No issue here, just wondering why a functionality I was so used to isn’t working any longer/didn’t catch this.

If you’re so used to it, that really makes a statement about your coding abilities that you might want to be a little more mindful of.

[editline]20th October 2013[/editline]

Check your code briefly before running.

Heh… I guess I shouldn’t say that I’m used to seeing the overflow protection, but rather Garry’s Mod has made me accustom to there being safety checks built in so I wouldn’t have to look as hard for loops while I’m coding. But I suppose you’re right, I should check before running.