Selaa lähdekoodia

Memory cleanup fixes

Ivan Safrin 9 vuotta sitten
vanhempi
sitoutus
4e1e0e19d9

+ 0 - 1
bindings/javascript/Polycode/RenderThread.js

@@ -10,7 +10,6 @@ RenderThread.JOB_DESTROY_SHADER = 9
 RenderThread.JOB_DESTROY_PROGRAM = 10
 RenderThread.JOB_DESTROY_SUBMESH_BUFFER = 11
 RenderThread.JOB_DESTROY_RENDER_BUFFER = 13
-RenderThread.JOB_SET_TEXTURE_PARAM = 14
 
 Duktape.fin(RenderThread.prototype, function (x) {
 	if (x === RenderThread.prototype) {

BIN
bindings/javascript/js_Polycode.pak


+ 0 - 1
bindings/lua/Polycode/RenderThread.lua

@@ -6,7 +6,6 @@ RenderThread.JOB_DESTROY_SHADER = 9
 RenderThread.JOB_DESTROY_PROGRAM = 10
 RenderThread.JOB_DESTROY_SUBMESH_BUFFER = 11
 RenderThread.JOB_DESTROY_RENDER_BUFFER = 13
-RenderThread.JOB_SET_TEXTURE_PARAM = 14
 
 function RenderThread:RenderThread(...)
 	local arg = {...}

BIN
bindings/lua/lua_Polycode.pak


+ 24 - 13
build/osx/TemplateApp/TemplateApp/AppDelegate.m

@@ -29,26 +29,37 @@
 
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
 {
-    app = new PolycodeTemplateApp(mainView);
-    timer = [NSTimer timerWithTimeInterval:(1.0f/60.0f)
-                                    target:self
-                                  selector:@selector(animationTimer:)
-                                  userInfo:nil
-                                   repeats:YES];
-    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
-    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
+	app = new PolycodeTemplateApp(mainView);
+	timer = [NSTimer timerWithTimeInterval:(1.0f/60.0f)
+									target:self
+								  selector:@selector(animationTimer:)
+								  userInfo:nil
+								   repeats:YES];
+	[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
+	[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
+}
+
+- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)theApplication
+{
+	delete app;
+	app = NULL;
+	return NSTerminateNow;
 }
 
 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
 {
-    return YES;
+	delete app;
+	app = NULL;
+	return YES;
 }
 
 - (void)animationTimer:(NSTimer *)timer
 {
-    if(!app->Update()) {
-        [[NSApplication sharedApplication] stop:self];
-    }
+	if(app) {
+		if(!app->Update()) {
+			[[NSApplication sharedApplication] stop:self];
+		}
+	}
 }
 
-@end
+@end

+ 1 - 2
build/osx/TemplateApp/TemplateApp/PolycodeTemplateApp.mm

@@ -14,7 +14,6 @@ PolycodeTemplateApp::PolycodeTemplateApp(PolycodeView *view) {
 
     core->addFileSource("archive", "hdr.pak");
     globalPool->loadResourcesFromFolder("hdr", true);
-
 	
 	scene = new Scene(Scene::SCENE_2D);
 	scene->useClearColor = true;
@@ -33,7 +32,7 @@ void PolycodeTemplateApp::handleEvent(Event *event) {
 }
 
 PolycodeTemplateApp::~PolycodeTemplateApp() {
-    
+	delete core;
 }
 
 bool PolycodeTemplateApp::Update() {

+ 1 - 0
include/polycode/core/PolyCocoaCore.h

@@ -173,6 +173,7 @@ namespace Polycode {
 		
 	protected:
 	
+		PAAudioInterface *audioInterface;
 		PolycodeView *glView;
 		uint64_t initTime;
 		bool retinaSupport;

+ 1 - 3
include/polycode/core/PolyRenderer.h

@@ -135,6 +135,7 @@ namespace Polycode {
 		public:
 			RenderThread();
 			 void setGraphicsInterface(Core *core, GraphicsInterface *graphicsInterface);
+			~RenderThread();
 			virtual void runThread();
 		
 			void beginFrame();
@@ -165,7 +166,6 @@ namespace Polycode {
 			static const int JOB_DESTROY_PROGRAM = 10;
 			static const int JOB_DESTROY_SUBMESH_BUFFER = 11;
 			static const int JOB_DESTROY_RENDER_BUFFER = 13;
-			static const int JOB_SET_TEXTURE_PARAM = 14;
 		
 		protected:
 		
@@ -211,8 +211,6 @@ namespace Polycode {
 		void destroyShaderPlatformData(void *platformData);
 		void destroySubmeshPlatformData(void *platformData);
 		
-		void setTextureParam(LocalShaderParam *param, Texture *texture);
-		
 		void setAnisotropyAmount(Number amount);
 		Number getAnisotropyAmount();
 		

+ 1 - 0
include/polycode/core/PolySoundManager.h

@@ -51,6 +51,7 @@ namespace Polycode {
 	class _PolyExport AudioInterface {
 		public:
 			AudioInterface();
+			virtual ~AudioInterface();
 			//void addToBuffer(int16_t *data, unsigned int count);
 			virtual void setMixer(AudioMixer *mixer);
 			AudioMixer *getMixer();		   

+ 4 - 2
src/core/PolyCocoaCore.mm

@@ -135,8 +135,9 @@ CocoaCore::CocoaCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen,
     renderer->setGraphicsInterface(this, interface);
     services->setRenderer(renderer);
     setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, retinaSupport);
-    
-    services->getSoundManager()->setAudioInterface(new PAAudioInterface());
+	
+	audioInterface = new PAAudioInterface();
+    services->getSoundManager()->setAudioInterface(audioInterface);
 }
 
 void CocoaCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, bool retinaSupport) {
@@ -332,6 +333,7 @@ String CocoaCore::executeExternalCommand(String command,  String args, String in
 
 CocoaCore::~CocoaCore() {
 	printf("Shutting down cocoa core\n");
+	delete renderer;
 	[glView setCore:nil];	
 	shutdownGamepad();
 	if(fullScreen) {

+ 1 - 0
src/core/PolyCore.cpp

@@ -63,6 +63,7 @@ namespace Polycode {
 		getScreenInfo(&defaultScreenWidth, &defaultScreenHeight, &_hz);
 	
 		coreResized = false;
+		renderer = NULL;
 		
 		this->aaLevel = aaLevel;
 		this->anisotropyLevel = anisotropyLevel;

+ 0 - 1
src/core/PolyCoreServices.cpp

@@ -94,7 +94,6 @@ CoreServices::~CoreServices() {
 	delete soundManager;
 	delete logger;
 	delete config;
-	delete renderer;
 
 	instanceMap.clear();
 	overrideInstance = NULL;

+ 12 - 12
src/core/PolyRenderer.cpp

@@ -380,6 +380,14 @@ void RenderThread::processDrawBuffer(GPUDrawBuffer *buffer) {
 	
 }
 
+RenderThread::~RenderThread() {
+	clearFrameQueue();
+	renderMutex->unlock();
+	delete renderMutex;
+	jobQueueMutex->unlock();
+	delete jobQueueMutex;
+}
+
 void RenderThread::lockRenderMutex() {
 	Services()->getCore()->lockMutex(renderMutex);
 }
@@ -419,13 +427,6 @@ void RenderThread::processJob(const RendererThreadJob &job) {
 			graphicsInterface->destroyProgramData(job.data);
 		}
 		break;
-		case JOB_SET_TEXTURE_PARAM:
-		{
-			LocalShaderParam *param = (LocalShaderParam*) job.data;
-			Texture *texture = (Texture*) job.data2;
-			param->data = (void*) texture;
-		}
-		break;
 		case JOB_DESTROY_SUBMESH_BUFFER:
 		{
 			graphicsInterface->destroySubmeshBufferData(job.data);
@@ -501,7 +502,10 @@ Renderer::Renderer(RenderThread *customThread) :
 }
 
 Renderer::~Renderer() {
-	
+	// finish up and quit
+	renderThread->threadRunning = false;
+	while(!renderThread->scheduledForRemoval) {}
+	delete renderThread;
 }
 
 void Renderer::setGraphicsInterface(Core *core, GraphicsInterface *graphicsInterface) {
@@ -549,10 +553,6 @@ void Renderer::destroyProgramPlatformData(void *platformData) {
 	renderThread->enqueueJob(RenderThread::JOB_DESTROY_PROGRAM, platformData);
 }
 
-void Renderer::setTextureParam(LocalShaderParam *param, Texture *texture) {
-	renderThread->enqueueJob(RenderThread::JOB_SET_TEXTURE_PARAM, (void*)param, (void*)texture);
-}
-
 
 void Renderer::destroyShaderPlatformData(void *platformData) {
 	renderThread->enqueueJob(RenderThread::JOB_DESTROY_SHADER, platformData);

+ 6 - 2
src/core/PolyResourceManager.cpp

@@ -919,9 +919,13 @@ ScriptResourceLoader::ScriptResourceLoader() {
 
 ScriptResourceLoader::~ScriptResourceLoader() {
 #ifndef NO_LUA
-	lua_close(luaState);
+	if(luaState) {
+		lua_close(luaState);
+	}
 #endif
-	duk_destroy_heap(duktapeContext);
+	if(duktapeContext) {
+		duk_destroy_heap(duktapeContext);
+	}
 }
 
 std::shared_ptr<Resource> ScriptResourceLoader::loadResource(const String &path, ResourcePool *targetPool) {

+ 3 - 0
src/core/PolySoundManager.cpp

@@ -129,6 +129,9 @@ void SoundManager::setAudioInterface(AudioInterface *audioInterface) {
 }
 
 
+AudioInterface::~AudioInterface() {
+}
+
 AudioInterface::AudioInterface() {
 }