otland / forgottenserver

A free and open-source MMORPG server emulator written in C++
https://otland.net
GNU General Public License v2.0
1.57k stars 1.05k forks source link

Memory leaks #4288

Open ArturKnopik opened 1 year ago

ArturKnopik commented 1 year ago

Before creating an issue, please ensure:

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](https://github.com/otland/forgottenserver/files/10356258/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 https://github.com/otland/forgottenserver/commit/92b35aa3ebe3660aaebdf2ea17df8f35f776350c https://stackoverflow.com/questions/59483862/c-lambda-function-cause-memory-leak
EPuncker commented 1 year ago

@ranisalt can you check this please?

ranisalt commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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

Zbizu commented 1 year ago

one confirmed leak is noted in #3552

ArturKnopik commented 1 year ago

helgrind.txt helgrind also report issues

ArturKnopik commented 1 year ago

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 commented 1 year ago

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 commented 3 months ago

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