Why are the enemies not appearing on the screen?

-fsanitize=address -static-libasan reports:

Score = 3

highscore = 3
state = 1

==147076==ERROR: AddressSanitizer: alloc-dealloc-mismatch (operator new vs free) on 0x506000080480
#0 0x5556e04bb1d7 in __interceptor_free (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0x9c1d7)
#1 0x5556e050899b in doPointsPods() (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xe999b)
#2 0x5556e05032d1 in logic() (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xe42d1)
#3 0x5556e050a924 in main (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xeb924)
#4 0x7f3b8e48ed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#5 0x7f3b8e48ee3f in __libc_start_main_impl ../csu/libc-start.c:392
#6 0x5556e042b7d4 in _start (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xc7d4)

0x506000080480 is located 0 bytes inside of 56-byte region [0x506000080480,0x5060000804b8)
allocated by thread T0 here:
#0 0x5556e04bce87 in operator new(unsigned long) (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0x9de87)
#1 0x5556e0508a08 in addPointsPod(int, int) (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xe9a08)
#2 0x5556e0504fdf in bulletHitFighter(Laser*) (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xe5fdf)
#3 0x5556e0504270 in doBullets() (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xe5270)
#4 0x5556e0503233 in logic() (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xe4233)
#5 0x5556e050a924 in main (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xeb924)
#6 0x7f3b8e48ed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: alloc-dealloc-mismatch (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0x9c1d7) in __interceptor_free
==147076==HINT: if you don’t care about these errors you may set ASAN_OPTIONS=alloc_dealloc_mismatch=0
==147076==ABORTING
alistair@alistair-EasyNote-TJ65:~/Programming/C++/sidescrollingspaceshooter/OV25HSE$ =================================================================
==147076==ERROR: AddressSanitizer: alloc-dealloc-mismatch (operator new vs free) on 0x506000080480
#0 0x5556e04bb1d7 in __interceptor_free (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0x9c1d7)
#1 0x5556e050899b in doPointsPods() (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xe999b)
#2 0x5556e05032d1 in logic() (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xe42d1)
#3 0x5556e050a924 in main (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xeb924)
#4 0x7f3b8e48ed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#5 0x7f3b8e48ee3f in __libc_start_main_impl ../csu/libc-start.c:392
#6 0x5556e042b7d4 in _start (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0xc7d4)

0x506000080480 is located 0 bytes inside of 56-byte region [0x506000080480,0x5060000804b8)
allocated by thread T0 here:
#0 0x5556e04bce87 in operator new(unsigned long) (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV25HSE/a.out+0x9de87)
#1 0x5556e0508a08 in addPointsPod(int, int) (/home/alistair/Programming/C++/HSEstair@alistair-EasyNote-TJ65:~/Programming/C++/sidescrollingspaceshooter/

is it something to do with the logic changing the gamestate starting on line 119 in main.cpp?

Compile and link your code with -g so that the sanitizer can output file line numbers.

What the sanitizer seems to be complaining about is that that you create the Laser objects with new in addPointsPod but delete it with free instead of delete in doPointsPods.

You are supposed to either use malloc/calloc and free (these will not call constructors and destructors) or new and delete (recommended in C++).

ok fixed that but its still crashing. gdb reports

Thread 1 “a.out” received signal SIGSEGV, Segmentation fault.
__strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
Download failed: Invalid argument. Continuing without source file ./string/../sysdeps/x86_64/multiarch/strlen-avx2.S.
74 ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
(gdb) bt
#0 __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
(gdb)

what am I doing wrong when I try and change gamestate?

Man, see at the line main.cpp/119:

if( (lives <=0) && (highscore) >= (newHighscore) )

highscore and newHighscore are two struct of type Highscores. And newHighscore is a pointer! You cannot compare a pure struct with other struct in this way, but you must compare their data member! Data member with data member and NOT struct with struct (or struct pointer)

For example:

the variable highscore you should use highscore.highscore[index] (variable name ‘dot’ data member name [ int index] ) to acess the member highscore of the struct highscore.

The names you choosed are too ambiguous. I suggest you to call the Highscore (the data from one player) like PlayerScore and the Highscores like just Highscore. Because this will remove all the ambiguity in the names and make it easy to read and understand what you did.

In that case, PlayerScore is just a single line of one single player and Highscore is the set of many players’ score! Think it in just pieces of a puzzle: you have the pieces at the same size but with different parts of a great picture! And the set of puzzle’s pieces make the great picture together.

Highscore keeps the array of PlayerScore (the set of lines) and PlayerScore keeps the infos about the final result of one single player (just one line per player).

Can you give an example of how the state change logic might look as I am struggling to keep track of the right variables.

Thank you kindly

I will clear the mind with some psychedelic music tonight and try again tomorrow.

hahaha try listen binaural beats, I do this some times.

I will give you a simple example based on the last post I did:

--your logic if the if(lives <=0 && highscore >= newHighscore) is logically correct, but, in syntax, is not! the variable highscore is a int and newHighscore is a struct pointer and a int and one struct like you thinking in this if example, they cannot be compared in this way! Being highscore a int you should compare it with another int. Being newHighscore a struct pointer you should compare only with other struct pointer (techinicly you can do the int and pointer, but ignore it here).

--so, what I undertood that you want to check if the current highscore is >= than the most recent highsocre which is the variable newHisghscore. To do this, you must access the member score of the newHighscore variable. Or, in this case, you are trying to do the follow pseudocode:

IF the player died AND the current score highscore is >= newHighscore->score, THEN, change to enter highscore screen.

Understand it now? Your logic is correct but the syntax is wrong. newHighscore->score is the score of the most recent score!

But, even changing the syntax, the code still seg fault. Why? This happens cause the newHighscore is made NULL inside the function addHighscore(). And at addHighscore() you don’t check if the newHighscore is valid or not null (a valid memory area).

See at the addHighscore() inside the second for(). The for goes from 0 to NUM_HIGHSCORES-1, but the last element of the newHighscores is the only with the member recent == 1. See it? This for never goes to the element of index NUM_HIGHSCORES cause the variable i goes from 0 to < NUM_HIGHSCORES! Or, goes from 0 to <= NUM_HIGHSCORES-1

Other bug you pass the stage.score to the addHighscore() but after calling resetStage(), resetStage() will set the stage.score to 0! So, the score will be nulled! You must decide which variable take care of the player score, if the stage.score or the highscore (the global int).

After correct all this things, you will see the screen to type the player name, it is bugged too, but I will let you work at it. Try to solve it and if you cannot, just ask me.

Good luck!

This shouldn’t even compile.

GCC says:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:119:52: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     if( (lives <=0) && (highscore) >= (newHighscore) )
                                                    ^

ok now I do

if( (lives <=0) && (highscore) >= (lastKnownHighscore))
{

			doNameInput();
			myGameState=ENTERHIGHSCORESCREEN;
			}
			else if
			( (lives<=0) && (highscore) <= (lastKnownHighscore))

			{
			myGameState=HIGHSCORESCREEN;
			}

and I get segfault, I am trying to get the value of the 1st array in the highscore table score value with

lastKnownHighscore=&highscores.highscore[0].score;
		

in addHighscore() for luup,

please I just want to compare highest gamescore with 1st array score in the highscore table and change state.

please show me how

Where should I look to set an integer value to the first entry in the high score table

I am now comparing 2 pointers to cause the gamestate logic change. one is the highhest score so far and the other is the first entry in the highscore table

.

whats causing the segfault?

Man, I wrote all the things you should do to fix the main bugs, you just should read all I wrote!

I will give up from this topic.

So, good luck in your journey!

Not sure if your github repository is up to date because the following line should not compile (unless you use -fpermissive).

lastKnownHighscore=&highscores.highscore[0].score; 

After fixing this line (by removing the &) the code will compile but, as samleo has already pointed out, this line will never actually get executed. This means that lastKnownHighscore will remain zero and newHighscore will remain null even after the addHighscore function has returned.

After running out of lives, the game crashes on line 1267 in FunctionDefinitions.cpp (inside the doNameInput function). You should be able to get this information from GDB, Valgrind or the sanitizers.

FunctionDefinitions.cpp:1267:12: runtime error: null pointer passed as argument 1, which is declared to never be null
ASAN:DEADLYSIGNAL
=================================================================
==4562==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f6f3531f676 bp 0x7fffba724fd0 sp 0x7fffba724758 T0)
    #0 0x7f6f3531f675 in strlen (/lib/x86_64-linux-gnu/libc.so.6+0x80675)
    #1 0x7f6f373daeec  (/usr/lib/x86_64-linux-gnu/libasan.so.3+0x3beec)
    #2 0x55e295b155ff in doNameInput /home/peter/Downloads/sidescrollingspaceshooter-main/FunctionDefinitions.cpp:1267
    #3 0x55e295b16ad0 in main /home/peter/Downloads/sidescrollingspaceshooter-main/main.cpp:122
    #4 0x7f6f352bf2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
    #5 0x55e295b07969 in _start (/home/peter/Downloads/sidescrollingspaceshooter-main/game+0x18969)

The reason for the crash is that you try to access newHighscore->name (and pass it to strlen) when newHighscore is null.

ok Im trying a different approach, drawring thehighscore entry screen if newHighScores is != NULL. My compilation is fine but can anyone tell me why pressing ctrl is not starting the game?

Your latest code doesn’t compile.

ok try it now

I’m getting a crash when I press lctrl and it doesn’t run with gdb, what is the cause of this?

Thanks

I get:

$ ./a.out

lCtrl key pressed!
AddressSanitizer:DEADLYSIGNAL

==322202==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55593cfa4d26 bp 0x7ffed58b0260 sp 0x7ffed58af9a8 T0)
==322202==The signal is caused by a READ memory access.
==322202==Hint: address points to the zero page.
#0 0x55593cfa4d26 in __sanitizer::internal_strlen(char const*) (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/a.out+0xb4d26)
#1 0x55593cf35727 in printf_common(void*, char const*, __va_list_tag*) (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/a.out+0x45727)
#2 0x55593cf36dc2 in __interceptor_vsprintf (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/a.out+0x46dc2)
#3 0x55593cfd8e19 in drawText(int, int, int, int, int, int, char*, …) /home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/FunctionDefinitions.cpp:984
#4 0x55593cfd9111 in drawHud /home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/FunctionDefinitions.cpp:1023
#5 0x55593cfd58e8 in draw /home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/FunctionDefinitions.cpp:502
#6 0x55593cfdb875 in main /home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/main.cpp:113
#7 0x7f20fcb73d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#8 0x7f20fcb73e3f in __libc_start_main_impl ../csu/libc-start.c:392
#9 0x55593cefc7d4 in _start (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/a.out+0xc7d4)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/home/alistair/Programming/C++/sidescrollingspaceshooter/OV24HSE/a.out+0xb4d26) in __sanitizer::internal_strlen(char const*)
==322202==ABORTING

The code still doesn’t compile.

FunctionDefinitions.cpp: In function ‘void drawHud()’:
FunctionDefinitions.cpp:1023:33: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
 drawText(10, 10, 255, 255, 255, "SCORE: %03d", stage.score);
                                 ^~~~~~~~~~~~~
FunctionDefinitions.cpp:977:54: note:   initializing argument 6 of ‘void drawText(int, int, int, int, int, int, char*, ...)’
 void drawText(int x, int y, int r, int g, int b, int align, char *format, ...)
                                                  ~~~~^~~~~

Ok I have it running now but I cant seem to trigger the highscore entry screen. Can you trigger the highscore entry screen?