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 issue when using cpp operate node , need help #102702

Open
njikmf opened this issue Feb 11, 2025 · 6 comments
Open

Memory issue when using cpp operate node , need help #102702

njikmf opened this issue Feb 11, 2025 · 6 comments

Comments

@njikmf
Copy link

njikmf commented Feb 11, 2025

Tested versions

4.2.2 stable

System information

Win10 , VS2022

Issue description

I want to port a cpp code using godot, so I will not use gdscript or gdextension. From Doc, gdextension can define new node ,then still need gdscript to operate it , add/remove;.

I added simple code to display a cube,

void SceneTree::initialize() {
	ERR_FAIL_NULL(root);
	MainLoop::initialize();
	root->_set_tree(this);

	MeshInstance3D *m_mesh = memnew(MeshInstance3D);
	BoxMesh *m_box_mesh = memnew(BoxMesh);
	root->add_child(m_mesh);
	m_mesh->set_position(Vector3(2, 0, 0));
	m_mesh->set_mesh(m_box_mesh);

}

This is ok, but the strange thing is, If I move these 5 line code to another file, I got memory issue.

A breakpoint instruction (__debugbreak() statement or a similar call) was executed in godot_windows.exe.

 	ntdll.dll!00007ffd3ce2f2d2()	Unknown	Non-user code. Cannot find or open the PDB file.
 	ntdll.dll!00007ffd3ce38092()	Unknown	Non-user code. Cannot find or open the PDB file.
 	ntdll.dll!00007ffd3ce3837a()	Unknown	Non-user code. Cannot find or open the PDB file.
 	ntdll.dll!00007ffd3ce3e001()	Unknown	Non-user code. Cannot find or open the PDB file.
 	ntdll.dll!00007ffd3cdd7442()	Unknown	Non-user code. Cannot find or open the PDB file.
 	ntdll.dll!00007ffd3cd547b1()	Unknown	Non-user code. Cannot find or open the PDB file.
 	igvk64.dll!00007ffccf00855c()	Unknown	Non-user code. Cannot find or open the PDB file.
 	igvk64.dll!00007ffccefee006()	Unknown	Non-user code. Cannot find or open the PDB file.
 	igvk64.dll!00007ffcce3acf94()	Unknown	Non-user code. Cannot find or open the PDB file.
>	godot_windows.exe!RenderingDeviceVulkan::_free_pending_resources(int p_frame) Line 8931	C++	Symbols loaded.
 	godot_windows.exe!RenderingDeviceVulkan::_begin_frame() Line 8722	C++	Symbols loaded.
 	godot_windows.exe!RenderingDeviceVulkan::swap_buffers() Line 8792	C++	Symbols loaded.
 	godot_windows.exe!RenderingServerDefault::_draw(bool p_swap_buffers, double frame_step) Line 99	C++	Symbols loaded.
 	godot_windows.exe!RenderingServerDefault::draw(bool p_swap_buffers, double frame_step) Line 389	C++	Symbols loaded.
 	godot_windows.exe!Main::iteration() Line 3690	C++	Symbols loaded.
 	godot_windows.exe!OS_Windows::run() Line 1474	C++	Symbols loaded.
 	godot_windows.exe!widechar_main(int argc, wchar_t * * argv) Line 182	C++	Symbols loaded.
 	godot_windows.exe!_main() Line 206	C++	Symbols loaded.
 	godot_windows.exe!main(int argc, char * * argv) Line 218	C++	Symbols loaded.
 	[External Code]		Annotated Frame

Then I enabled the Address Sanitizer,

Godot Engine v4.2.2.stable.custom_build.15073afe3 - https://godotengine.org
Vulkan API 1.3.289 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce GTX 1660

=================================================================
==12716==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x1256f91d9fe0 at pc 0x7ff761a4c767 bp 0x004fdf3af070 sp 0x004fdf3af070
WRITE of size 8 at 0x1256f91d9fe0 thread T0
==12716==WARNING: Failed to use and restart external symbolizer!
    #0 0x7ff761a4c766 in NodePath::NodePath D:\code\cpp\godot-422-gitee\core\string\node_path.h:49
    #1 0x7ff767f89872 in MeshInstance3D::MeshInstance3D D:\code\cpp\godot-422-gitee\scene\3d\mesh_instance_3d.h:47
    #2 0x7ff76a687e2a in ArchlordNode::init D:\code\cpp\godot-422-gitee\archlord\archlord_node.cpp:6
    #3 0x7ff766eff7fa in SceneTree::initialize D:\code\cpp\godot-422-gitee\scene\main\scene_tree.cpp:461
    #4 0x7ff7620903b9 in OS_Windows::run D:\code\cpp\godot-422-gitee\platform\windows\os_windows.cpp:1470
    #5 0x7ff76157e758 in widechar_main D:\code\cpp\godot-422-gitee\platform\windows\godot_windows.cpp:180
    #6 0x7ff76157e3ed in _main D:\code\cpp\godot-422-gitee\platform\windows\godot_windows.cpp:204
    #7 0x7ff76157e858 in main D:\code\cpp\godot-422-gitee\platform\windows\godot_windows.cpp:218
    #8 0x7ff763e740bf in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #9 0x7ffed5767343 in BaseThreadInitThunk+0x13 (C:\WINDOWS\System32\KERNEL32.DLL+0x180017343)
    #10 0x7ffed6e026b0 in RtlUserThreadStart+0x20 (C:\WINDOWS\SYSTEM32\ntdll.dll+0x1800526b0)

0x1256f91d9fe0 is located 0 bytes after 1632-byte region [0x1256f91d9980,0x1256f91d9fe0)
allocated by thread T0 here:
    #0 0x7ffe9b67f2a8 in _asan_wrap__CrtIsValidHeapPointer+0x2068 (C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\bin\HostX64\x64\clang_rt.asan_dynamic-x86_64.dll+0x18004f2a8)
    #1 0x7ff76158f869 in Memory::alloc_static D:\code\cpp\godot-422-gitee\core\os\memory.cpp:75
    #2 0x7ff76158f70a in operator new D:\code\cpp\godot-422-gitee\core\os\memory.cpp:40
    #3 0x7ff76a687e19 in ArchlordNode::init D:\code\cpp\godot-422-gitee\archlord\archlord_node.cpp:6
    #4 0x7ff766eff7fa in SceneTree::initialize D:\code\cpp\godot-422-gitee\scene\main\scene_tree.cpp:461
    #5 0x7ff7620903b9 in OS_Windows::run D:\code\cpp\godot-422-gitee\platform\windows\os_windows.cpp:1470
    #6 0x7ff76157e758 in widechar_main D:\code\cpp\godot-422-gitee\platform\windows\godot_windows.cpp:180
    #7 0x7ff76157e3ed in _main D:\code\cpp\godot-422-gitee\platform\windows\godot_windows.cpp:204
    #8 0x7ff76157e858 in main D:\code\cpp\godot-422-gitee\platform\windows\godot_windows.cpp:218
    #9 0x7ff763e740bf in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #10 0x7ffed5767343 in BaseThreadInitThunk+0x13 (C:\WINDOWS\System32\KERNEL32.DLL+0x180017343)
    #11 0x7ffed6e026b0 in RtlUserThreadStart+0x20 (C:\WINDOWS\SYSTEM32\ntdll.dll+0x1800526b0)

SUMMARY: AddressSanitizer: heap-buffer-overflow D:\code\cpp\godot-422-gitee\core\string\node_path.h:49 in NodePath::NodePath
Shadow bytes around the buggy address:
  0x1256f91d9d00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1256f91d9d80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1256f91d9e00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1256f91d9e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1256f91d9f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x1256f91d9f80: 00 00 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa
  0x1256f91da000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x1256f91da080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1256f91da100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1256f91da180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1256f91da200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb

Can some one told me what's the reason?

Steps to reproduce

add these 5 line code to display a cube,

	MeshInstance3D *m_mesh =memnew(MeshInstance3D);
	BoxMesh *m_box_mesh = memnew(BoxMesh);
	root->add_child(m_mesh);
	m_mesh->set_position(Vector3(2, 0, 0));
	m_mesh->set_mesh(m_box_mesh);

if inside void SceneTree::initialize() , everything is ok, cube will be displayed;
if outside void SceneTree::initialize() , I put it in a custom class , xxxx::init , it crashed

Minimal reproduction project (MRP)

any empty project

@AThousandShips
Copy link
Member

This is for reporting bugs, this sounds like a support question, please ask in the other community channels for support questions

@njikmf
Copy link
Author

njikmf commented Feb 11, 2025

I ask for help because I am not sure is these 5 line code incorrect, or Godot code has memory issue.

@AThousandShips
Copy link
Member

Try using the appropriate memory management for your types, it should be Ref<BoxMesh> not BoxMesh *, but if that doesn't help please ask in other channels to confirm that you are using things correctly, and if you are please return here

@njikmf
Copy link
Author

njikmf commented Feb 11, 2025

I tested, Ref not the problem, Ref class has a constructor to accept raw pointer. I really don't understand why just 5 simple line code cause crash..
These 5 line code is converted from GDscript:

func _ready():
	var mi=MeshInstance3D.new()
	var bm=BoxMesh.new()
	mi.set_position(Vector3(1,0,0))
	mi.set_mesh(bm)
	add_child(mi)

this works well in GDScript, so anyone can explain me why it not work in cpp?

@AThousandShips
Copy link
Member

Did you try:

	MeshInstance3D *m_mesh =memnew(MeshInstance3D);
	Ref<BoxMesh> m_box_mesh = memnew(BoxMesh);
	root->add_child(m_mesh);
	m_mesh->set_position(Vector3(2, 0, 0));
	m_mesh->set_mesh(m_box_mesh);

But please ask in the other community channels to make sure this is a bug and not user error, and if it is a bug please provide an example project for testing

@huwpascoe
Copy link
Contributor

Just posting a link to this thread in the discord isn't exactly in the spirit of asking for help...

Without seeing the full source I can only guess, and my guess is that your class ArchlordNode has been put there without any understanding of the engine architecture - SceneTree::initialize is not where your own init should be injected.

Follow this guide for adding internal custom c++
https://docs.godotengine.org/en/stable/contributing/development/core_and_modules/custom_modules_in_cpp.html

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