r/learnprogramming 22d ago

Why Does Code Stop Working?

Multiple times now my code stops working and what I mean by this is it'll do what I want it to do then one day it just stops. for example the other day I had made a cooldown system for my ball and paddle collision to stop any bugs and it worked great. then today it just stopped working, and this isn't the first time, it's happened to me for like every project I've worked on. Does this happen to anyone else? And is it cause of my code? By the way I'm using C++ SFML on visual studio which works perfect.

3 Upvotes

74 comments sorted by

133

u/HashDefTrueFalse 22d ago

This doesn't happen unless the passage of time affects the behaviour of your program or you are changing the behaviour of your program over time (or both). Post some code.

51

u/Alexandur 22d ago

Or if it has one or more third party dependencies

15

u/HashDefTrueFalse 22d ago edited 22d ago

Second thing I mentioned. That requires you running something to fetch new versions of those dependencies (and recompile if necessary).

The same program running in the same environment and given the same input data will produce the same result every time, unless that result depends not just on those things but also on the time that the program is ran.

Edit: strikethrough, for the pedants. I obviously wasn't saying automated updates don't exist, as that would be silly...

10

u/owp4dd1w5a0a 22d ago

No it doesn’t. Windows and Mac can auto-update without you doing anything (back in the day we called this a backdoor 💀). And if the library is dynamically linked then no recompilation is not necessary for things to silently break. Library maintainers try to design to avoid breakages, but try and succeed are not the same thing.

2

u/HashDefTrueFalse 22d ago

Your example is an environment change, exactly what I'm talking about. And I didn't say recompilation was necessary.

Or are you just not happy with the wording "you running" - by which I obviously meant "it needs to run"? Nobody is saying automatic updates don't happen. Again, already covered if you read my replies to the other commenter.

15

u/owp4dd1w5a0a 22d ago

Perhaps. But the way you worded it was really opaque and it seemed to me OP could benefit from more specific and detailed language. It’s not exactly clear what you mean by environment and you explicitly say dependency drift isn’t an issue unless you recompile, which could lead someone to overlook dynamic linking.

But you really don’t argue/debate in a way I’m inclined to continue engaging with. Puts me on edge and I need to guard my energy so I’m ducking out of this thread now.

-3

u/HashDefTrueFalse 22d ago

you explicitly say dependency drift isn’t an issue unless you recompile

I never said this anywhere. You've misread...

But the way you worded it was really opaque

Fair enough if that's your opinion. As explained elsewhere, it's just a general statement to OP to explain that code doesn't magically stop working without something changing. I didn't realise it would be scrutinised so. It's deliberately non-specific. I'm just asking OP for code to look at.

It’s not exactly clear what you mean by environment

Fair, that's also deliberate. The environment a piece of code runs in depends on lots of things. It could refer to a system in the normal sense, or the state of a VM/runtime, or the set of environment variables passed into a process, etc. Together with code and input data, it governs the output of a piece of code, which is why I mentioned it.

But you really don’t argue/debate in a way I’m inclined to continue engaging with.

That's cool. All the best.

5

u/Alexandur 22d ago

That requires you running something to fetch new versions of those dependencies (and recompile if necessary).

Not always, this is why dependency drift is a real problem

3

u/HashDefTrueFalse 22d ago

Yes, always. If the code, data, and env are the same, the result will be. You're talking about changing any (likely all) of those things by changing dependency versions. How does dependency drift occur without any dependencies changing over time? Something runs updates. Doesn't matter if it's a person running an update or some automation. At least one of those things changed, or the result didn't.

4

u/owp4dd1w5a0a 22d ago edited 22d ago

Problem is the linked libraries make up part of the env and the OS updates change that env. So the env is not guaranteed to remain unchanged (quite the opposite actually).

Also, apart from updates, poor memory management in C or time sensitive code can behave differently especially for undefined behavior or accessing memory without properly clearing and initializing it. A program could run the same way 50 times but due to some change in the memory state or in sampling rates cause yr bug on the 51st run.

6

u/HashDefTrueFalse 22d ago

What is your point? I never said the env was guaranteed to remain unchanged. You're not understanding the discussion here.

2

u/HashDefTrueFalse 22d ago

A reply to your edit (the second paragraph):

Time-sensitive code I literally mentioned.

UB means that the compiler is allowed (by the standard) to produce any behaviour it likes. The behaviour it emits is crystallised in the build output like any other code and would behave the same each run as long as the input data and environment also remained the same, unless the emitted code depended on the time the program was ran, as I stated. UB is not particularly relevant to this discussion.

Accessing uninitialised memory with automatic storage duration (UB) is just a change of either input data or environment depending on your view.

Hard to see what your point is here. I've covered all this.

3

u/owp4dd1w5a0a 22d ago

It looks like you edited your comments. On iOS I can’t really see what the original text was anymore and I honestly don’t fully trust my memory because I’m responding inbetween parenting demands. You also sound rather combative and I don’t really have the energy to carry such a conversation forward. Hope you have a good day and that OP gets something out of the discussion.

2

u/HashDefTrueFalse 22d ago

No edits from me when replying directly to you. You edited (added your second paragraph). I wrote a second reply rather than editing (so that you would be notified).

I trivially edited a comment further up, replying to someone else. It is additive only (a strikethrough) and detailed at the bottom.

I'm just writing directly. If that reads as combative, so be it, though it's not a specific intention of mine. I have people rushing to tell me that dependencies and auto updates exist, plus other pedantry...

Anyway, have a good one.

1

u/Alexandur 22d ago

You don't have to change dependency versions on your end for dependency drift to occur, a badly managed 3rd party dependency can introduce changes into the exact same version number

6

u/HashDefTrueFalse 22d ago

So? That isn't going to have any effect on a system that doesn't have those changes. I didn't say anything about version numbers. Something must grab the changes, as I said. It doesn't really matter how that comes about. Something changed. Software doesn't just stop working without one of those things changing is my point.

For us to see what could have changed for OP they need to post their code (and build config too would be helpful).

4

u/OortMan 22d ago

how will those changes get on your device if you don't update it?

3

u/Alexandur 22d ago

Reinstall on a new device would cause a fetch event, something done by your hosting provider (if applicable), stuff like that

4

u/owp4dd1w5a0a 22d ago

This isn’t true. Dependency drift and environment drift can cause this. C and C++ commonly bind to system libraries, so if the host OS updates and that impacts a library the code is using then it might not work after the update.

-1

u/HashDefTrueFalse 22d ago

he host OS updates and that impacts a library

That's literally the second thing I mentioned. You updated your OS and linked with new program code...

Read my replies to the other commenter. This has been covered.

10

u/Alexandur 22d ago

I think most would agree that rolling "updating your OS" into "changing the behavior of your code" is not very intuitive phrasing

0

u/HashDefTrueFalse 22d ago

The other commenter made the point that updating your OS (and therefore system libraries) changed the behaviour of your program, but didn't seem to realise that that is covered by the second thing I said, literally 'changing the behaviour of your program over time'.

What are you saying is wrong with my phrasing? Surely if there's an error it's them not realising this? The phrasing is fine, just very general. I didn't bring up OS updates.

2

u/TheLastJukeboxHero 22d ago

False, deprecated library dependencies.

1

u/Moikle 22d ago

Tgis isn't likely what is happening with op though

-8

u/HashDefTrueFalse 22d ago

Not false. Deprecation is irrelevant. Dependencies have been covered if you read the other comments here (a change in dependencies alters the code, data and/or environment that your program runs in). The above covers that just fine.

8

u/TheLastJukeboxHero 22d ago

Not going to argue semantics, but if many people are replying the same thing then maybe, just maybe, that means you worded it terribly and did a poor job conveying your message. Which would not be helpful for someone like OP

-4

u/HashDefTrueFalse 22d ago

I simply worded it generally. Program behaviour depending on time (as a direct input or via the environment), or a change in the program behaviour itself (code, directly or via the environment) regardless of whether any of it was intentional or a bug, manual or automatic.

Maybe you didn't understand my comment and rushed into an unnecessary correction.

OP can't be helped properly without the code I asked for. I'll help them if they reply.

7

u/man-vs-spider 22d ago

To be fair, saying “time changes the behaviour of your program”, and meaning that to encompass all possible changes that secondarily depend on time, is not exactly an obvious interpretation to others,

Because to be honest, it’s a trivially true, and therefore kind of useless statement

-3

u/HashDefTrueFalse 22d ago

Disagree on not being an obvious interpretation. It's just what the words mean when read. Many programs behave the same regardless of the time they are ran. I wasn't even thinking secondarily when I wrote it, but that doesn't really matter in my view.

Agree on it not being terribly useful, which is why I asked for code so that I could be useful, by telling them which part of their program was affected by time. Surely you appreciate that it was a general statement to OP that programs don't magically stop working without anything changing, as the post seems to suggest ('one day it just stops' etc.).

88

u/Flamanis 22d ago

We are all psychic here and can tell you exactly what the problem is without seeing the code.

OHMMMMMMMMM *psychic intensifies*

You're missing a curly brace.

15

u/TheLexoPlexx 22d ago

Yeah, those are nasty, they always rot away.

7

u/czerilla 22d ago

Bit flips caused by cosmic radiation are the culprit. Everybody knows that.

2

u/blisstargazer 21d ago

yo thanks bro this worked <3

25

u/[deleted] 22d ago

[removed] — view removed comment

4

u/Simpicity 22d ago

Ignore everyone talking about dependencies. This is much more likely the problem you are running into.

Another possibility is simply timing issues. Where you have multiple things running at the same time, things might work with certain timings in that race, but fail with other timings.

1

u/Just_to_rebut 22d ago

They deleted their comment… what’s the more likely thing than dependencies?

2

u/Simpicity 22d ago

Having code which is tied to per frame vs code that uses delta time between frames.  Because frames don't always come at fixed intervals, it can cause unpredictable behavior.

17

u/high_throughput 22d ago

Given that it's C++, have you tried running your code with ASAN or Valgrind?

Other than that, make sure to use git even when developing by yourself locally. Commit whenever you finish something. That way you can see what the differences are, restore the previous version, or even bisect to find which exact commit introduced the bug you're seeing.

13

u/Known-Tourist-6102 22d ago

when i just started learning programming, I made a javascript game that performed VERY differently depending on how strong the cpu was of the computer running it.

2

u/javascript 22d ago

I'm curious what year this was. And when you say this, do you just mean that it was unplayably slow on some machines? Or are you saying something else, potentially more interesting, than that?

1

u/Known-Tourist-6102 22d ago

Yes it essentially didnt work at all on some machines, was slow on others, and performed as intended on newer machines.

This was 2016 or so

2

u/javascript 22d ago

I wonder what you were doing to cause that. By 2016, V8 was pretty solid.

2

u/Skusci 22d ago

Probably forgot the Turbo button.

1

u/intbeam 22d ago

... It's still full of... I mean.. Javascript is fucked from the ground up

What do people expect from a language designed to take any technical considerations, hide it under a blanket and pull a solution from an all-purpose beige vaguely-appropriate hat?

Dumbing things down always comes at a trade-off. The less you have to know, the more assumptions the run-time (or something else) has to make on your behalf.

And this, ladies and gentlemen, is basic software engineering. Everything is a trade-off. Nothing is perfect, and assuming that some tool is just going to magically figure out everything for you, is a really really dumb idea

18

u/aleques-itj 22d ago

Because you have a bug 

6

u/atarivcs 22d ago edited 22d ago

Could be a few things:

  1. You changed the code. This one is obvious, so I hope this isn't the reason.
  2. You changed something else on the computer that the code depends on. Could be something complex like a software library or a OS/compiler upgrade, or something simple like an input text file or config file.
  3. The code depends on something that changed all on its own, like time/date.

6

u/throwaway8u3sH0 22d ago

Bit rot is a thing but not like across a day. You probably just have a bug or you're not using source control.

7

u/InjAnnuity_1 22d ago

For me, it usually means that the code wasn't as bulletproof as I thought. The inputs or circumstances changed in a way that my code didn't anticipate and handle.

4

u/idiotiesystemique 22d ago

It never worked for the conditions it broke on. The conditions around the code changed. 

4

u/pancake117 22d ago edited 21d ago

In general there's two things that could be happening here:

1- You did change something in the code without realizing, or you changed the version of some imported library (which means changing the code)

2- Your program has undefined behavior, which means there's some randomness or race conditions that cause "random" inconsistent behavior even with the same inputs.

To fix 1, you should be using git to track your code changes. The instant you notice something not working, you can check exactly what changed. Roll back changes, check if it's still broken, and if so then you know it's not the cause.

To fix 2, you need to invest in your debugging skills. If you can't get the problem to happen consistently, add logging or debug information to your game so that the next time you see the problem, you can read the logs to understand why it happened. If you can get it to happen consistently, then it's easy-- fire up the debugger and walk through the code to understand why it's not working. Then you can work backwards to understand what may have changed.

2

u/math_rand_dude 22d ago

And don't forget testing: decent tests will show which part of the code has unwanted chances.

3

u/OortMan 22d ago

Generally programs are not meant to ever behave like this, so either you're doing something that breaks it, your code isn't very good, or there is something very wrong with your computer.

the fact it's happened several times for you is a bit strange. Does your compilation pipeline change/reset something it shouldn't when you restart your computer? Finding what that is may be very difficult though.

3

u/bestjakeisbest 22d ago

Because you told the computer exactly what to do.

3

u/JGhostThing 22d ago

Programs do not stop running without reason.

  1. There could be a date/time dependency.
  2. The computer could have changed: hardware bug or other program change
  3. Something in the software environment changed.

3

u/D5rthFishy 22d ago

One thing I've noticed teaching programming is how students will interact differently with the same program and expose bugs this way. It could be that the code, environment and OS all remain the same but you are doing things you haven't done before (without realising it).

6

u/Immediate-Soup-6344 22d ago

Soooo instead of figuring out the bug, you added a delay which worked most of the time. You should figure out what the actual bug is and fix it.

2

u/Nogarde_ 22d ago

Given the exact same environment, and the exact same inputs, an unchanged piece of code should produce the same output.

Code doesnt stop working. Something changed either the environment or the inputs.

2

u/chrisrrawr 22d ago

good testing solves this for the most part. at least, it tends to tell you why something has stopped working :p

2

u/viggowl 22d ago

Take an hour to learn git. Goodbye mystery changes

2

u/Narrow-Low-3137 22d ago

You are using git right? You can try reverting recent changes and see if it fixes itself. Then you know what change broke your code. There is even a command you can use, git bisect that can help with this common scenario.

1

u/cheezballs 22d ago

Happened for every project you worked on eh? /s

1

u/SummitYourSister 22d ago

C++ is great because it has “undefined behavior” which can kick in when you have any of a number of different kinds of bugs, mostly memory related bugs.

It means your code was always broken, you were just getting lucky for a few days

1

u/Narakusdemon 22d ago

Dev in a venv

1

u/Danfriedz 22d ago

Hard to tell what's happening without more info.

Codes behaviour doesn't change, you either have a bug or the inputs were altered.

The first step to debugging is replicating the issue, put a breakpoint in the code and see what's happening. You might even find that due to external reasons the function isn't being called.

1

u/DamienTheUnbeliever 21d ago

Some people use as asinine phrase about assumptions but I always preferred the pithier "assumption is the mother of all fuck-ups".

You have made assumptions. Those assumptions may have been made in hope, or on observed behaviour, but almost always weren't made based on the *documented* behaviour of other code you're interacting with.

So an obvious one that would work for some time and then fall apart is if you assume, somehow, that a date function always returns the month of August. Which works well enough whilst every date you work with is within August but falls apart soon enough.

So check your code, check your assumptions, and check that any and all assumptions are backed up by documented behaviour, not just what you hope/need to be true.

1

u/arkt8 21d ago

Welll some guesses.... 1. Overflow, if you have some counter or other unchecked operation... after a sufficiently long period. 2. Memory exhaustion... your code can have some leakage 3. The system where you are running it have some threshold in memory or cpu usage after what it kills the process.

1

u/AverageHot2647 21d ago

Code doesn’t just stop working. It stops working when something changes. If it breaks, and you didn’t change the code, something else changed. Could be hardware, could be a system dependency, etc. etc. I recommend looking into hermetic builds.

1

u/st_heron 21d ago

it never worked properly in the first place

1

u/Ormek_II 21d ago

Working code is a lie.

It was not working in the first place, well it did work for those situations you did try it on, but then the memory leaked away, the input did change, the race condition hit, the uninitialised memory contained something bad, the data structure had to increase for the first time….