[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16558] trunk/blender/source/gameengine: Fix (harmless) error print about GameLogic.globalDict being lost.

Brecht Van Lommel brechtvanlommel at pandora.be
Tue Sep 16 21:25:54 CEST 2008


Revision: 16558
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16558
Author:   blendix
Date:     2008-09-16 21:25:35 +0200 (Tue, 16 Sep 2008)

Log Message:
-----------
Fix (harmless) error print about GameLogic.globalDict being lost. Also
fixed some memory leaks in this code and simplified it.

Modified Paths:
--------------
    trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Application.cpp
    trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Application.h
    trunk/blender/source/gameengine/GamePlayer/ghost/GPG_ghost.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp

Modified: trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Application.cpp
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Application.cpp	2008-09-16 19:10:58 UTC (rev 16557)
+++ trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Application.cpp	2008-09-16 19:25:35 UTC (rev 16558)
@@ -115,6 +115,7 @@
 	  m_cursor(GHOST_kStandardCursorFirstCursor),
 	  m_engineInitialized(0), 
 	  m_engineRunning(0), 
+	  m_isEmbedded(false),
 	  m_ketsjiengine(0),
 	  m_kxsystem(0), 
 	  m_keyboard(0), 
@@ -128,8 +129,7 @@
 	  m_blendermat(0),
 	  m_blenderglslmat(0),
 	  m_pyGlobalDictString(0),
-	  m_pyGlobalDictString_Length(0),
-	  m_isEmbedded(false)
+	  m_pyGlobalDictString_Length(0)
 {
 	fSystem = system;
 }
@@ -138,6 +138,12 @@
 
 GPG_Application::~GPG_Application(void)
 {
+    if(m_pyGlobalDictString) {
+		delete m_pyGlobalDictString;
+		m_pyGlobalDictString = 0;
+		m_pyGlobalDictString_Length = 0;
+	}
+
 	exitEngine();
 	fSystem->disposeWindow(m_mainWindow);
 }
@@ -680,7 +686,8 @@
 		initPythonConstraintBinding();
 		initMathutils();
 
-		/* Restore the dict */
+		// Set the GameLogic.globalDict from marshal'd data, so we can
+		// load new blend files and keep data in GameLogic.globalDict
 		loadGamePythonConfig(m_pyGlobalDictString, m_pyGlobalDictString_Length);
 		
 		m_sceneconverter->ConvertScene(
@@ -718,13 +725,15 @@
 
 void GPG_Application::stopEngine()
 {
-	// get the python dict and convert to a string for future use
-	char *marshal_buffer;
-	m_pyGlobalDictString_Length = saveGamePythonConfig(&marshal_buffer);
-	if (m_pyGlobalDictString_Length) {
-		m_pyGlobalDictString = static_cast<char *> (malloc(m_pyGlobalDictString_Length));
-		memcpy(m_pyGlobalDictString, marshal_buffer, m_pyGlobalDictString_Length);
-	} 
+	// GameLogic.globalDict gets converted into a buffer, and sorted in
+	// m_pyGlobalDictString so we can restore after python has stopped
+	// and started between .blend file loads.
+	if(m_pyGlobalDictString) {
+		delete m_pyGlobalDictString;
+		m_pyGlobalDictString = 0;
+	}
+
+	m_pyGlobalDictString_Length = saveGamePythonConfig(&m_pyGlobalDictString);
 	
 	// when exiting the mainloop
 	exitGamePythonScripting();

Modified: trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Application.h
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Application.h	2008-09-16 19:10:58 UTC (rev 16557)
+++ trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Application.h	2008-09-16 19:25:35 UTC (rev 16558)
@@ -74,29 +74,6 @@
 			bool StartGameEngine(int stereoMode);
 			void StopGameEngine();
 
-			char*
-		GetPyGlobalDictMarshal()
-		{ 
-			return m_pyGlobalDictString;
-		};
-		
-			void
-		SetPyGlobalDictMarshal( char* pyGlobalDictString, int length )
-		{
-			if (m_pyGlobalDictString && m_pyGlobalDictString != pyGlobalDictString)
-				free(m_pyGlobalDictString);
-			
-			m_pyGlobalDictString = pyGlobalDictString;
-			m_pyGlobalDictString_Length = length;
-		};
-		
-			int
-		GetPyGlobalDictMarshalLength()
-		{ 
-			return m_pyGlobalDictString_Length;
-		};
-
-
 protected:
 	bool	handleWheel(GHOST_IEvent* event);
 	bool	handleButton(GHOST_IEvent* event, bool isDown);

Modified: trunk/blender/source/gameengine/GamePlayer/ghost/GPG_ghost.cpp
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/ghost/GPG_ghost.cpp	2008-09-16 19:10:58 UTC (rev 16557)
+++ trunk/blender/source/gameengine/GamePlayer/ghost/GPG_ghost.cpp	2008-09-16 19:25:35 UTC (rev 16558)
@@ -301,8 +301,6 @@
 	GHOST_TUns32 fullScreenHeight= 0;
 	int fullScreenBpp = 32;
 	int fullScreenFrequency = 60;
-	char* pyGlobalDictString = NULL; /* store python dict data between blend file loading */
-	int pyGlobalDictString_Length = 0;
 	GHOST_TEmbedderWindowID parentWindow = 0;
 
 
@@ -658,10 +656,6 @@
 						
 						titlename = maggie->name;
 						
-						// Set the GameLogic.globalDict from marshal'd data, so we can load new blend files
-						// abd keep data in GameLogic.globalDict
-						app.SetPyGlobalDictMarshal(pyGlobalDictString, pyGlobalDictString_Length);
-						
 						// Check whether the game should be displayed full-screen
 						if ((!fullScreenParFound) && (!windowParFound))
 						{
@@ -790,12 +784,7 @@
 							}
 						}
 						app.StopGameEngine();
-						
-						// GameLogic.globalDict has been converted into a buffer
-						// store in pyGlobalDictString so we can restore after python has stopped and started.
-						pyGlobalDictString = app.GetPyGlobalDictMarshal();
-						pyGlobalDictString_Length = app.GetPyGlobalDictMarshalLength();
-						
+
 						BLO_blendfiledata_free(bfd);
 					}
 				} while (exitcode == KX_EXIT_REQUEST_RESTART_GAME || exitcode == KX_EXIT_REQUEST_START_OTHER_GAME);
@@ -814,11 +803,6 @@
 
 	free_nodesystem();
 
-	if (pyGlobalDictString) {
-		free(pyGlobalDictString);
-		pyGlobalDictString = NULL;
-	}
-	
 	return error ? -1 : 0;
 }
 

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2008-09-16 19:10:58 UTC (rev 16557)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2008-09-16 19:25:35 UTC (rev 16558)
@@ -1393,17 +1393,21 @@
 			PyObject* pyGlobalDictMarshal = PyMarshal_WriteObjectToString(	pyGlobalDict ); 
 #endif
 			if (pyGlobalDictMarshal) {
-				marshal_length= PyString_Size(pyGlobalDictMarshal);
 				// for testing only
 				// PyObject_Print(pyGlobalDictMarshal, stderr, 0);
-				*marshal_buffer = PyString_AsString(pyGlobalDictMarshal);
+
+				marshal_length= PyString_Size(pyGlobalDictMarshal);
+				*marshal_buffer = new char[marshal_length + 1];
+				memcpy(*marshal_buffer, PyString_AsString(pyGlobalDictMarshal), marshal_length);
+
+				Py_DECREF(pyGlobalDictMarshal);
 			} else {
 				printf("Error, GameLogic.globalDict could not be marshal'd\n");
 			}
-			Py_DECREF(gameLogic);
 		} else {
 			printf("Error, GameLogic.globalDict was removed\n");
 		}
+		Py_DECREF(gameLogic);
 	} else {
 		printf("Error, GameLogic failed to import GameLogic.globalDict will be lost\n");
 	}
@@ -1412,20 +1416,26 @@
 
 int loadGamePythonConfig(char *marshal_buffer, int marshal_length)
 {
-	PyObject* gameLogic = PyImport_ImportModule("GameLogic");
 	/* Restore the dict */
 	if (marshal_buffer) {
-		PyObject* pyGlobalDict = PyMarshal_ReadObjectFromString(marshal_buffer, marshal_length);
-		if (pyGlobalDict) {
-			PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
-			return 1;
+		PyObject* gameLogic = PyImport_ImportModule("GameLogic");
+
+		if (gameLogic) {
+			PyObject* pyGlobalDict = PyMarshal_ReadObjectFromString(marshal_buffer, marshal_length);
+
+			if (pyGlobalDict) {
+				PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
+				Py_DECREF(gameLogic);
+				return 1;
+			} else {
+				Py_DECREF(gameLogic);
+				PyErr_Clear();
+				printf("Error could not marshall string\n");
+			}
 		} else {
-			PyErr_Clear();
-			printf("Error could not marshall string\n");
-		}
-	} else {
-		printf("Error, GameLogic failed to import GameLogic.globalDict will be lost\n");
-	}	
+			printf("Error, GameLogic failed to import GameLogic.globalDict will be lost\n");
+		}	
+	}
 	return 0;
 }
 





More information about the Bf-blender-cvs mailing list