cocos creater hot update restart causes crash
knowledge points
- jsb_websocket_server.cpp socket used in the game
- Inspector_socket_server.cc V8 engine for remote debugging native socket server
demand background
- Game engine: cocos creater 2.4.3
- Calling cc.game.restart() after the hot update of the game causes a crash
- The online crash rate index rose sharply when the game was hotly updated
crash site
Accidentally got a model that can be displayed
The crash stack is as follows:
crash analysis
-
According to the crash stack, it can be seen that jsb_websocket_server is called
-
As a result of the breakpoint, you can see that the entry point of the crash is the method in the callback, as shown in the figure below
-
So it is speculated that after cc.game.restart(), the websocket is not released cleanly. After restarting, the wild pointer receives a callback, resulting in a crash
-
Since we didn't use the websocket of cocos creator, we decided to remove the websocket
-
The first step solution: Directly annotate the callback functions registered by jsb_websocket_server, and enter the game without crashing
-
The second step solution: Check the source code to know whether to open the socket is controlled by a macro. As shown below
-
so we just need to add the macro definition and change USE_SOCKET to 0 as shown below
Extended Thinking
-
We didn't use websocket in our project, so we commented out websocket directly. Since the crash is websocket_server, in theory, if you need to use websocket, set USE_WEBSOCKET_SERVER to 0. You can also remember to check why the crash has not come yet. Those who have ideas can leave a message to exchange
-
After removing the websocket, you can also remotely debug native, so I found out the principle of remote debugging again. code show as below:
-
From the figure above, we know that the remote debugging native function uses socket_setver in the V8 engine. What caused the crash was the websocket in the game engine
-
You can also see it in the breakpoint running callback. When accessing the debug link, you can enter the breakpoint, you can try it yourself
At this point, the problem and solution of the crash caused by hot update game restart are over
Students who have questions are welcome to leave a message to discuss