legacyclonk / LegacyClonk

The LegacyClonk engine and the c4group command line tool.
https://clonkspot.org/lc-en
Other
83 stars 18 forks source link

ReloadDef can cause a segfault #79

Closed bratkartoff closed 2 years ago

bratkartoff commented 2 years ago

I changed this definition

[DefCore]
Width=64
Height=64
Offset=32,32
...

to this one

[DefCore]
Width=1
Height=1
# Offset removed
ColorByOwner=1
...

called ReloadDef and got this

[21:09:11] Reloading CICN from FlagChooserIcons.c4d
[21:09:12] WARNING: eval: passing 3 parameters, but only 1 are used (in DeserializeObjects, Hazard.c4d/System.c4g/Serialize.c:193:40)
[21:09:16] C4AulScriptEngine linked - 117233 lines, 1 warning, 0 errors
[21:09:16] -> ReloadDef(CICN)
[21:09:16]  = 1

Thread 1 "clonk" received signal SIGSEGV, Segmentation fault.
0x0000555555848a45 in CStdDDraw::Blit (this=this@entry=0x555555a4f300, sfcSource=sfcSource@entry=0x555571ba13a0, fx=<optimized out>, fy=0, fwdt=<optimized out>, fhgt=<optimized out>, sfcTarget=sfcTarget@entry=0x555555aa29e0, tx=<optimized out>, ty=170, twdt=<optimized out>, thgt=<optimized out>, fSrcColKey=fSrcColKey@entry=true, pTransform=pTransform@entry=0x0, noScalingCorrection=noScalingCorrection@entry=false) at StdDDraw2.cpp:732
732             if (iTexSize != pTex->iSize)
(gdb) bt
#0  0x0000555555848a45 in CStdDDraw::Blit (this=this@entry=0x555555a4f300, sfcSource=sfcSource@entry=0x555571ba13a0, fx=<optimized out>, fy=0, fwdt=<optimized out>, fhgt=<optimized out>, 
    sfcTarget=sfcTarget@entry=0x555555aa29e0, tx=<optimized out>, ty=170, twdt=<optimized out>, thgt=<optimized out>, fSrcColKey=fSrcColKey@entry=true, pTransform=pTransform@entry=0x0, 
    noScalingCorrection=noScalingCorrection@entry=false) at StdDDraw2.cpp:732
#1  0x0000555555849382 in CStdDDraw::Blit (this=this@entry=0x555555a4f300, sfcSource=sfcSource@entry=0x555571ba13a0, fx=<optimized out>, fy=<optimized out>, fwdt=<optimized out>, 
    fhgt=<optimized out>, sfcTarget=sfcTarget@entry=0x555555aa29e0, tx=tx@entry=343, ty=<optimized out>, twdt=<optimized out>, thgt=22, fSrcColKey=true, pTransform=0x0, 
    noScalingCorrection=false) at StdDDraw2.cpp:647
#2  0x00005555556412d7 in C4Facet::Draw (this=this@entry=0x555578c2a1e0, cgo=..., fAspect=fAspect@entry=true, iPhaseX=iPhaseX@entry=0, iPhaseY=iPhaseY@entry=0, 
    fTransparent=fTransparent@entry=true, scale=scale@entry=1) at C4Facet.cpp:130
#3  0x00005555556415f2 in C4Facet::DrawClr (this=this@entry=0x555578c2a1e0, cgo=..., fAspect=fAspect@entry=true, dwClr=<optimized out>)
    at C4Facet.cpp:158
#4  0x00005555556f2036 in C4MenuItem::DrawElement (this=0x555578c29920, cgo=...) at C4Menu.cpp:166
#5  0x00005555556a160e in C4GUI::Container::Draw (cgo=..., this=<optimized out>) at C4GuiContainers.cpp:42
#6  C4GUI::Container::Draw (this=<optimized out>, cgo=...) at C4GuiContainers.cpp:32
#7  0x00005555556a197f in C4GUI::Window::Draw (this=0x555578c7e830, cgo=...) at C4GuiContainers.cpp:288
#8  0x00005555556a160e in C4GUI::Container::Draw (cgo=..., this=<optimized out>) at C4GuiContainers.cpp:42
#9  C4GUI::Container::Draw (this=<optimized out>, cgo=...) at C4GuiContainers.cpp:32
#10 0x00005555556a197f in C4GUI::Window::Draw (this=this@entry=0x555578c90050, cgo=...) at C4GuiContainers.cpp:288
#11 0x00005555556a35a3 in C4GUI::Dialog::Draw (this=this@entry=0x555578c90050, cgo=...) at C4GuiDialogs.cpp:524
#12 0x00005555556f52ce in C4Menu::Draw (cgo=..., this=0x555578c90050) at C4Menu.cpp:796
#13 C4Menu::Draw (this=0x555578c90050, cgo=...) at C4Menu.cpp:783
#14 0x0000555555841e4d in C4Viewport::DrawMenu (this=0x5555789efc60, cgo=...) at C4Viewport.cpp:1010
#15 0x0000555555842209 in C4Viewport::DrawOverlay (this=0x5555789efc60, cgo=...) at C4Viewport.cpp:865
#16 0x000055555584263d in C4Viewport::Draw (this=0x5555789efc60, cgo=..., fDrawOverlay=<optimized out>) at C4Viewport.cpp:1124
#17 0x00005555558428df in C4Viewport::Execute (this=0x5555789efc60) at C4Viewport.cpp:1170
#18 0x00005555556912d4 in C4GraphicsSystem::Execute (this=0x5555559b3418 <Game+15640>) at C4GraphicsSystem.cpp:193
#19 0x00005555555e7d8c in C4Application::Execute (this=0x555555a0c460 <Application>) at C4Application.cpp:431
#20 0x0000555555858905 in CStdApp::HandleMessage (this=this@entry=0x555555a0c460 <Application>, iTimeout=iTimeout@entry=4294967295, fCheckTimer=fCheckTimer@entry=true)
    at StdXApp.cpp:405
#21 0x00005555558589ba in CStdApp::Run (this=this@entry=0x555555a0c460 <Application>) at StdXApp.cpp:286
#22 0x00005555555e5095 in main (argc=2, argv=0x7fffffffdbe8) at C4WinMain.cpp:270
maxmitti commented 2 years ago

Is there an open menu which is using the reloaded definition as icon?

bratkartoff commented 2 years ago

Yes, there is

     var tmp = CreateObject(CICN);
    ...
    AddMenuItem(GetName(point),"SelectFlagpole2",GetID(),crew,point->GetProcess(),ObjectNumber(point),"",4,tmp)
                                                                                                            ^
maxmitti commented 2 years ago

Ok, that was vital information.

In this specific case the problem seems to come from here: https://github.com/legacyclonk/LegacyClonk/blob/master/src/C4Object.cpp#L3099-L3103

In simple cases the icon simply references the graphics from the object definition. The referenced graphics are then deleted when the definition is reloaded and in the next frame the invalid graphics are tried to be used for rendering.

The question is how to fix it reasonably. I first have to analyze further what ReloadDef does …