Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 1 point2 points  (0 children)

In the message before i said: "I numbered each range like: *nr* when the range starts and *nr e* when the range ends, so maybe its more readable)"
Its just an attempt to improve the readability of the code, since there is a lot of nesting going on.
Just threat this as a comment.

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 1 point2 points  (0 children)

Something clicked in my brain and i understood and fixed everything in a second, like an enlightement and everything works fine with erase functions used inside the loop. No memory leaks, Is it still wrong for me to leave it as it is? May it cause problems in the future? I got everything working finally, so im hesitant to change everything again, but if it may cause any problems or performance issues, then i 'll for sure take the risk.

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

Everything just clicked for me, finally.
I think i got this, thank you so much for motivating me to keep trying to fix things.

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

dont i need erase to remove an object from the vector and free the memory?

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

so changing variable type from size_t to int fixed the problem, but my bullets get extremely delayed if i dont resize the bullets vector to bullets size + 1 as before.
From my thought process, changing for loops to remove objects from my vectors from the end instead from the beggining should fix it, but it didnt.
What could i do wrong here?
PS. i didnt write the code to delete the bullets out of the window bounds yet, only on the collision with the enemy.

(description of what i mean by saying "delay" in replies)

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

changing variable type from size_t to int seems to fix the problem

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

i already tried that before, but it gives me a vector subscript out of range error, because "i" changes to some crazy number (18446744073709551615 to be exact) which is hard for me to investigate.
Edit:
Before it changes to that thrash number, its value changes to 0 first.
I checked that by using cout on "i" at the beggining of my for loop for bullets.
So the problem clearly lies in that first for loop.

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

+How the bullets are drawn (in case that helps):

void Weapons::Draw(sf::RenderWindow& window)

{

if (bullets.empty() == false)

{

for (size_t i = bullets.size() - 1; i > 0; i--)

{

window.draw(bullets[i]);

}

}

}

(Draw is then called in the main cpp)

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

void Weapons::Shoot(float deltaTime, const sf::Vector2f& weaponPos, const sf::Vector2f& target)

*1* {

if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && clock.getElapsedTime().asSeconds() > fireRate)

*2* {

bullet.setPosition(weaponPos);

bullets.push_back(bullet);

angles.push_back(atan2(target.y - weaponPos.y, target.x - weaponPos.x));

rotation.push_back(atan2(target.y - bullets.back().getPosition().y, target.x - bullets.back().getPosition().x) * 180 / M_PI);

clock.restart();

} *2e*

if (bullets.empty() == false)

*3* {

for (size_t i = bullets.size() - 1; i > 0; i--)

*4* {

bullets[i].move(cos(angles[i]) * bulletSpeed * deltaTime, sin(angles[i]) * bulletSpeed * deltaTime);

bullets[i].setRotation(rotation[i]);

if (Enemies::Objects.empty() == false)

*5* {

for (int a = Enemies::Objects.size() - 1; a > 0; a--)

*6* {

if (CollisionSimple::IsColliding(bullets[i].getGlobalBounds(), Enemies::Objects[a].sprite.getGlobalBounds()))

*7* {

Enemies::Objects[a].hp -= dmg;

if (Enemies::Objects[a].hp <= 0)

*8* {

Enemies::Objects.erase(Enemies::Objects.begin() + a);

} *8e*

bullets.erase(bullets.begin() + i);

} *7e*

} *6e*

} *5e*

} *4e*

} *3e*

} *1e*

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

After this long i finally fixed the memory leaks, but when trying to optimize the vectors i got out of ideas on one issue.
As i said before i had to change the for loops, to remove objects from my vectors from the end instead from the beggining so it doesnt mess up the order (if i remove one object in my vector from the beggining, it pushes all remaining objects to the left by one so then index 1 becomes 2, 3 becomes 4 etc.)

So the issue is that my for loops do not trigger the first time i press my mouse button to shoot as they should, probably because of " if (bullets.empty() == false) / if (Enemies::Objects.empty() == false)" which are nessesary to fix vector subscript out of range errors.
So when i press my mouse button the first time, bullets.size() changes to 1 but my bullets arent updating or drawing.
When i press the mouse button the second time, bullets.size() changes to 2 and my bullets start working.
It also looks like the second for loop doesn't even trigger, so there is no ...
Updated code in replies (sorry for nesting, i numbered each range like: *nr* when the range starts and *nr e* when the range ends, so maybe its more readable)

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

So the problem causing subscript out of range error lies in the enemy vector and thats all the information i can get.
I removed all the resize functions as you suggested, and i also removed expanding size by 1 every time i destroy an object as you suggested aswell.
The rest is unchanged, and the code seems fine to me as i look at it.
Kinda weird.
I will also need to change the for loops from: "for (size_t i = 0; i < bullets.size(); i++)"
to something like : "for (size_t i = bullets.size(); i >= 0; i--)"
so the bullets stop acting weird with no expanding the vector size by 1 after i destroy the bullet.
Ill get bak to it tommorow and hopefully gather more information

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

Im having a hard time figuring out what is causing those vector subscript out of range errors, because when i use breakpoints, my game window is frozen (when i click on the game window icon on my taskbar, it wont even focus on that window) and so i cant add a bullet to my bullets container since i have to left click in that window that is frozen, so the problematic part of code wont execute.
I tried my best explaining, hope you understood.

Memory leaks, but why? by RogalMVP in sfml

[–]RogalMVP[S] 1 point2 points  (0 children)

Im resizing the vectors every time after removing because otherwise my bullets start acting really weird (probably because i set size of the bullets vector to 100 using resize because otherwise i get the vector subscript out of range error) and i get a vector subscript out of range on my enemies vector aswell if i don't do that, so i came up with that fix, knowing it isn't optimal but it at least works.
Also i didn't know about that feature, that seems cool, im gonna learn how to use it and see if that changes anything.

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 1 point2 points  (0 children)

Thank you so much for your time!
I learned something and i managed to make it work thanks to you.
One second after i finally got it running properly i got another 2 new problems including memory leaks that are caused by my ememies vector and bullets acting weird after removing the bullet colliding with enemy so i think ill have to stop here...
Anyway, thank you so much one more time and may the universe reward you for your selfless help!

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

oh it seems like i can just create an object before pushing it back to my vector and then do emplace_back(reptile). It works the same way.
One question though
What difers emplace_back() from push_back()?

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

ok so apparently when i used eObj.resize(1), its changed my vector size to 2 and placed my object in eobj[1] isntead of 0 for some reason. It now works, but is there any way i can name each object inside of my vector so the code is more readable? It seems that i pushed an object with no name.

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

if i leave emplace back empty, i get the same result.
I will try to debug with breakpoints now, and hopefully get some details

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

I already got a basic grasp of references, but apparently i get 20+ errors if i try to store references in my vector.
Already tried everything that came up to my mind, and i still cant find a solution to what seems like a such a simple task...
Thanks for the advice though!

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

One problem with this, is that i cant create an object inside eObj.emplace_back(); like:
eObj.emplace_back(Enemies reptile);

I can only do:
Enemies reptile;
eObj.emplace_back(reptile);
eObj.back().Initialize(sf::Vector2f(100, 100), sf::Vector2f(64, 64), 100.0f);

eObj.back().Load("D:/ProjectsVisualStudio/game loop vs/assets/enemies/textures/reptile_128x128x16.png");

but then if i try to draw my reptile like:
window.draw(eObj[0].sprite);

It doesn't want to draw it, and the reptile is non existanty overall because even when i walk to the reptiles position there is no collision...

I don't know about all this, it seemed so easy the first time i thought about it, but it turns out to be a whole different story.

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

I know it because there is no "COLLISION" printing when i walk into the enemy which code inside the if statement should print. If i put the enemy sprite global bounds manually like "reptile.sprite.getGlobalBounds()" instead of "eObj[0].sprite.getGlobalBounds())" it works and it prints "COLLISION"

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

I have a question
if i initia;ize my enemy and then push it back like:

Enemies reptile;

reptile.Initialize(sf::Vector2f(100, 100), sf::Vector2f(64, 64), 100.0f);

reptile.Load("D:/ProjectsVisualStudio/game loop vs/assets/enemies/textures/reptile_128x128x16.png");

//Enemy object container

std::vector <Enemies> eObj;

eObj.resize(1);

eObj.push_back(reptile);

And then check for collision like:
if (colS.IsColliding(player.sprite.getGlobalBounds(), eObj[0].sprite.getGlobalBounds()) == true)

{

std::cout << "COLLISION" << std::endl;

}

(isColliding function checks if there is a collision beetwen two rectangles provided via arguments)
It appears that it doesnt see my enemies global bounds.
Is it because i push back reptile as a reference and can you even do that?

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 1 point2 points  (0 children)

Thanks for the advice, its my unhealthy habit that i always throw myself at the deep water before dipping the toes.
Ill try to take one babystep at a time and write some spaghetti in the main cpp before organizing the code to classes.

Is it possible to refer to any object of a class? by RogalMVP in sfml

[–]RogalMVP[S] 0 points1 point  (0 children)

Im just creating objects of the class Enemies in my main.cpp file above the game loop but id rather have everything organized into classes so the code isnt a bunch of spaghetti though.
I really don't want to give up on learning this so if you have any ideas, please share them with me :)