Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oh, look, a memory leak! #27

Open
ReiquelApplegate opened this issue Apr 18, 2024 · 18 comments
Open

oh, look, a memory leak! #27

ReiquelApplegate opened this issue Apr 18, 2024 · 18 comments

Comments

@ReiquelApplegate
Copy link
Contributor

In weapons.cpp:

w=this; //Oh, look, a memory leak. The new instruction is making something on the heap, but this circumvents removing it.

@ReiquelApplegate
Copy link
Contributor Author

ewMagic has this problem many times

@wilsonpe66
Copy link

There is a lot of copy pasta for that line :)

image

@wilsonpe66
Copy link

I think all of the following logic cout be put into is own fuction to avoid copy pasta here.

weapon *w=NULL;
					
					if(id==ewMagic)
					{
						w=new weapon(*this);
						Lwpns.add(w);
						dead=0;
					}
					else
					{
						w=this; //Oh, look, a memory leak. The new instruction is making something on the heap, but this circumvents removing it. 
					}
					
					w->o_tile = ref_o_tile;
					w->tile = ref_o_tile;
					w->dir = 3-w->dir;
					w->doAutoRotate(true);
					
					if(w->id != wWind)
					{
						w->id = wRefMagic; w->convertType(true);
						if ( do_animation ) 
						{
							if((w->dir==1)||(w->dir==2))
							w->flip ^= 3;
						}
					}
					if ( do_animation ) 
					{
						w->tile=w->o_tile;
						
						if(w->dir&2)
						{
							if(w->frames>1)
							{
							w->tile+=w->frames;
							}
							else
							{
							++w->tile;
							}
						}
					}
					w->ignoreHero=false;
					w->ignorecombo=((int32_t(checky)&0xF0)+(int32_t(checkx)>>4));
					w->y=(int32_t(checky)&0xF0)+check_y_ofs;
					w->x=(int32_t(checkx)&0xF0)+check_x_ofs;

@wilsonpe66
Copy link

see the following warning

image

@wilsonpe66
Copy link

This is a problem

if(id==ewMagic)
					{
						w=new weapon(*this);
						Lwpns.add(w);
						dead=0;
					}

the application is heap allocating here but it is not be clean up in w pointer goes out of scope.

				{
					weapon *w=this;
					bool isLocalHeapAllocated = false;

					if(id==ewMagic)
					{
						w=new weapon(*this);
						Lwpns.add(w);
						dead=0;
						isLocalHeapAllocated = true;
					}

					w->o_tile = ref_o_tile;
					w->tile = ref_o_tile;
					w->dir = 3-w->dir;
					w->doAutoRotate(true);
					
					if(w->id != wWind)
					{
						w->id = wRefMagic; w->convertType(true);
						if ( do_animation ) 
						{
							if((w->dir==1)||(w->dir==2))
							w->flip ^= 3;
						}
					}
					if ( do_animation ) 
					{
						w->tile=w->o_tile;
						
						if(w->dir&2)
						{
							if(w->frames>1)
							{
							w->tile+=w->frames;
							}
							else
							{
							++w->tile;
							}
						}
					}
					w->ignoreHero=false;
					w->ignorecombo=((int32_t(checky)&0xF0)+(int32_t(checkx)>>4));
					w->y=(int32_t(checky)&0xF0)+check_y_ofs;
					w->x=(int32_t(checkx)&0xF0)+check_x_ofs;

					if (isLocalHeapAllocated)
					{
						delete w;
					}
				}

@ZelRai
Copy link
Collaborator

ZelRai commented Sep 6, 2024 via email

@wilsonpe66
Copy link

well there are 8 location to change. And I have analyzed a lot of the code base and found object life times and encapsulation to be very weak.

In the past I have tryied clean it up on my one forks but there still so many more issues.

@wilsonpe66
Copy link

Also it is dangerous to use memset and memcpy on non trival stucts and classes.

See https://en.cppreference.com/w/cpp/language/classes#Trivial_class

@wilsonpe66
Copy link

I will creeate a pr for this issue.

@wilsonpe66
Copy link

There is an issue I have to try to resolve after all

Lwpns.add(w);

@wilsonpe66
Copy link

I hate globals :( I am back to the drawing board for now.

@wilsonpe66
Copy link

Did anyone try making the sprite_list counter and std::atomic and see if that can help?

I am going to try an attack the problem from that angle

@ZelRai
Copy link
Collaborator

ZelRai commented Sep 6, 2024 via email

@wilsonpe66
Copy link

I in my most local change and getting 100 frame per second boost at least.

@wilsonpe66
Copy link

If remove the frame cap, get like 2000 frames per second.

@wilsonpe66
Copy link

If I remove the frame cap, in release build plays get like 4000 to 6000 frames per second. Before I would get like 450 frame per second.

@wilsonpe66
Copy link

I have created my pr #29

@ZelRai
Copy link
Collaborator

ZelRai commented Sep 7, 2024

Nice work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants