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

Memory leaks #4288

Open
2 tasks done
ArturKnopik opened this issue Jan 5, 2023 · 12 comments
Open
2 tasks done

Memory leaks #4288

ArturKnopik opened this issue Jan 5, 2023 · 12 comments
Labels
priority: critical Issues with this label should be resolved as quickly as possible

Comments

@ArturKnopik
Copy link
Contributor

ArturKnopik commented Jan 5, 2023

Before creating an issue, please ensure:

  • This is a bug in the software that resides in this repository, and not a
    support matter (use https://otland.net/forums/support.16/ for support)
  • This issue is reproducible without changes to the C++ code in this repository

Steps to reproduce (include any configuration/script required to reproduce)

cmake -DCMAKE_BUILD_TYPE=Debug ..
make

valgrind --leak-check=full ./tfs
wait for full startup
ctrl+c

Expected behaviour

no memory leak

Actual behaviour

memory leaks
leaks.txt

Environment

VirtualBox
artur@artur-VirtualBox:/media/sf_forgottenserver_AK$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Codename: jammy

NOTES

Possible source of leaks
92b35aa
https://stackoverflow.com/questions/59483862/c-lambda-function-cause-memory-leak

@EPuncker
Copy link
Contributor

EPuncker commented Jan 6, 2023

@ranisalt can you check this please?

@ranisalt
Copy link
Member

ranisalt commented Jan 6, 2023

@EPuncker definitely, when I return from vacation by the end of the month :) it's one of my main objectives around here, fixing these leaks

@ArturKnopik
Copy link
Contributor Author

These leaks are at server startup and shutdown, I haven't checked how it looks from the player's perspective (when the player moves, performs actions, etc...), we have a lot of lambdas that have not been checked

@Zbizu
Copy link
Contributor

Zbizu commented Jan 6, 2023

These leaks are at server startup and shutdown, I haven't checked how it looks from the player's perspective (when the player moves, performs actions, etc...), we have a lot of lambdas that have not been checked

what about reload? does it leak too?

@ArturKnopik
Copy link
Contributor Author

ArturKnopik commented Jan 6, 2023

not tested
will be nice to have some list of features to test with description how to test it if needed
i expect really big list

@Zbizu
Copy link
Contributor

Zbizu commented Jan 6, 2023

@ArturKnopik
from what I see in the lambda commit itself:

  1. going to sleep in bed
  2. send guild motd to player
  3. connection opening and closing
  4. parse packet and handling individual packets (sending instructions to game thread)
  5. connection::internalSend
  6. creature walk
  7. "extra swing" (stairhop?)
  8. creature death
  9. creature::addCondition
  10. 3x g_scheduler.addEvent(createSchedulerTask(walkDelay, [=, id = getID()] () { g_game.forceRemoveCondition(id, type); }));
  11. databasetasks::runtask
  12. checkSpawn cycle
  13. raid system
  14. login/relog
  15. game/player cpp files
  16. globalevents lua register(?)
  17. world light update

@ArturKnopik
Copy link
Contributor Author

ArturKnopik commented Jun 4, 2023

@ranisalt how vacation? :)
In my opinion 'Critical' label should be added to this issue

@EPuncker EPuncker added the priority: critical Issues with this label should be resolved as quickly as possible label Jun 4, 2023
@Zbizu
Copy link
Contributor

Zbizu commented Jun 4, 2023

one confirmed leak is noted in #3552

@ArturKnopik
Copy link
Contributor Author

helgrind.txt
helgrind also report issues

@ArturKnopik
Copy link
Contributor Author

ArturKnopik commented Jun 7, 2023

Funny fact... we have Database singleton that is initialized only... 2 times...


Breakpoint 1 at 0x909b8: file /media/sf_forgottenserver_AK/src/database.cpp, line 14.

(gdb) r

Starting program: /media/sf_forgottenserver_AK/tfs 

[Thread debugging using libthread_db enabled]

Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".



Breakpoint 1, Database::Database (this=0x555555adfb70 <g_databaseTasks+16>) at /media/sf_forgottenserver_AK/src/database.cpp:14

14	Database::Database() { std::cout << "KRET ctor Database::Database()" << std::endl; }

(gdb) bt

#0  Database::Database (this=0x555555adfb70 <g_databaseTasks+16>)

    at /media/sf_forgottenserver_AK/src/database.cpp:14

#1  0x000055555559d87b in DatabaseTasks::DatabaseTasks (this=<optimized out>, 

    this=<optimized out>) at /media/sf_forgottenserver_AK/src/databasetasks.h:24

#2  0x00005555555d6a6b in __static_initialization_and_destruction_0 (__initialize_p=1, 

    __priority=65535) at /media/sf_forgottenserver_AK/src/otserv.cpp:28

#3  0x00005555555dd18b in _GLOBAL__sub_I_g_databaseTasks ()

    at /media/sf_forgottenserver_AK/src/otserv.cpp:369

#4  0x00007ffff6a29ebb in call_init (env=<optimized out>, argv=0x7fffffffdf08, argc=1)

    at ../csu/libc-start.c:145

#5  __libc_start_main_impl (main=0x55555559ea39 <main(int, char**)>, argc=1, 

    argv=0x7fffffffdf08, init=<optimized out>, fini=<optimized out>, 

    rtld_fini=<optimized out>, stack_end=0x7fffffffdef8) at ../csu/libc-start.c:379

#6  0x0000555555593315 in _start ()

(gdb) c

Continuing.

KRET ctor Database::Database()

KRET Ctor Dispatcher()

[New Thread 0x7ffff53dc640 (LWP 2775)]

[New Thread 0x7ffff4bdb640 (LWP 2776)]

The Forgotten Server - Version 1.5



Compiled with GNU C++ version 11.3.0

Compiled on Jun  7 2023 23:02:43 for platform x64

Linked with LuaJIT 2.1.0-beta3 for Lua support



A server developed by The Forgotten Server Team

Visit our forum for updates, support, and resources: https://otland.net/.



>> Loading config

>> Loading RSA key 

>> Establishing database connection...[Switching to Thread 0x7ffff53dc640 (LWP 2775)]



Thread 2 "tfs" hit Breakpoint 1, Database::Database (this=0x555555adfa60 <Database::getInstance()::instance>) at /media/sf_forgottenserver_AK/src/database.cpp:14

14	Database::Database() { std::cout << "KRET ctor Database::Database()" << std::endl; }

(gdb) bt

#0  Database::Database (this=0x555555adfa60 <Database::getInstance()::instance>)

    at /media/sf_forgottenserver_AK/src/database.cpp:14

#1  0x000055555559c989 in Database::getInstance ()

    at /media/sf_forgottenserver_AK/src/database.h:29

#2  0x000055555559f61d in mainLoader (services=0x7fffffffdcc0)

    at /media/sf_forgottenserver_AK/src/otserv.cpp:194

#3  0x000055555559ea36 in operator() (__closure=0x555555b19b90)

    at /media/sf_forgottenserver_AK/src/otserv.cpp:75

#4  0x00005555555af830 in std::__invoke_impl<void, main(int, char**)::<lambda()>&>(std::__invoke_other, struct {...} &) (__f=...) at /usr/include/c++/11/bits/invoke.h:61

#5  0x00005555555ad39c in std::__invoke_r<void, main(int, char**)::<lambda()>&>(struct {...} &) (

    __fn=...) at /usr/include/c++/11/bits/invoke.h:111

#6  0x00005555555aa1ae in std::_Function_handler<void(), main(int, char**)::<lambda()> >::_M_invoke(const std::_Any_data &) (__functor=...) at /usr/include/c++/11/bits/std_function.h:290

#7  0x000055555589784a in std::function<void ()>::operator()() const (this=0x555555b24d90)

    at /usr/include/c++/11/bits/std_function.h:590

#8  0x0000555555897684 in Task::operator() (this=0x555555b24d80)

    at /media/sf_forgottenserver_AK/src/tasks.h:23

#9  0x0000555555896cde in Dispatcher::threadMain (this=0x555555adfc20 <g_dispatcher>)

    at /media/sf_forgottenserver_AK/src/tasks.cpp:37

#10 0x00005555555da4cf in std::__invoke_impl<void, void (Dispatcher::*)(), Dispatcher*> (

    __f=@0x555555b24a70: (void (Dispatcher::*)(Dispatcher * const)) 0x555555896bc6 <Dispatcher::threadMain()>, __t=@0x555555b24a68: 0x555555adfc20 <g_dispatcher>)

    at /usr/include/c++/11/bits/invoke.h:74

#11 0x00005555555da069 in std::__invoke<void (Dispatcher::*)(), Dispatcher*> (

    __fn=@0x555555b24a70: (void (Dispatcher::*)(Dispatcher * const)) 0x555555896bc6 <Dispatcher::threadMain()>) at /usr/include/c++/11/bits/invoke.h:96

#12 0x00005555555d9a53 in std::thread::_Invoker<std::tuple<void (Dispatcher::*)(), Dispatcher*> >::_M_invoke<0ul, 1ul> (this=0x555555b24a68) at /usr/include/c++/11/bits/std_thread.h:253

#13 0x00005555555d8e66 in std::thread::_Invoker<std::tuple<void (Dispatcher::*)(), Dispatcher*> >::operator() (this=0x555555b24a68) at /usr/include/c++/11/bits/std_thread.h:260

#14 0x00005555555d79be in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (Dispatcher::*)(), Dispatcher*> > >::_M_run (this=0x555555b24a60) at /usr/include/c++/11/bits/std_thread.h:211

#15 0x00007ffff6edc2b3 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6

#16 0x00007ffff6a94b43 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442

#17 0x00007ffff6b26a00 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81

(gdb) 



@ArturKnopik
Copy link
Contributor Author

im back in time to commit from:
Aug 24, 2021 - leaks are here
Feb 6, 2020 - leaks are here
Apr 26, 2019 - leaks are here

@ranisalt ranisalt mentioned this issue Jun 2, 2024
3 tasks
EvilHero90 pushed a commit that referenced this issue Jun 5, 2024
The first leak in #4288 is related to handle leaking, I couldn't really reproduce how mysql_init is called without mysql_close not being called on the line before, but using a unique_ptr with a custom deleter fixes the leak.

Using unique_ptr for MYSQL_RES also fixes eventual leaks that happen on queries.
@ranisalt
Copy link
Member

ranisalt commented Jun 8, 2024

Hey @ArturKnopik can you update the list? We fixed a bunch of leaks recently :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: critical Issues with this label should be resolved as quickly as possible
Projects
None yet
Development

No branches or pull requests

4 participants