Skip to content

Commit

Permalink
Merge pull request #586 from devnexen/script_load_mem_leak_fix
Browse files Browse the repository at this point in the history
couple of surface/script mem leak fixes proposal.
  • Loading branch information
stalkerg authored May 19, 2022
2 parents 44c2517 + 5c9f0b9 commit 95a0a65
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
26 changes: 12 additions & 14 deletions lib/xgraph/xgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ int XGR_Screen::init(int flags_in)
}

void XGR_Screen::create_surfaces(int width, int height) {
XGR_ScreenSurface = new uint8_t[width * height] {0};
XGR_ScreenSurface2D = new uint8_t[width * height] {0};
XGR_ScreenSurface2DRgba = new uint32_t[width * height] {0};
XGR_ScreenSurface.reset(new uint8_t[width * height] {0});
XGR_ScreenSurface2D.reset(new uint8_t[width * height] {0});
XGR_ScreenSurface2DRgba.reset(new uint32_t[width * height] {0});

std::cout<<"XGR32_ScreenSurface = SDL_CreateRGBSurface"<<std::endl;
XGR32_ScreenSurface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
Expand Down Expand Up @@ -310,9 +310,6 @@ void XGR_Screen::destroy_surfaces() {

SDL_UnlockSurface(XGR32_ScreenSurface);

delete[] XGR_ScreenSurface;
delete[] XGR_ScreenSurface2D;
delete[] XGR_ScreenSurface2DRgba;
SDL_FreeSurface(XGR32_ScreenSurface);

sdlTexture = nullptr;
Expand Down Expand Up @@ -884,27 +881,27 @@ uint8_t* XGR_Screen::get_active_render_buffer() {
}

uint8_t* XGR_Screen::get_default_render_buffer() {
return XGR_ScreenSurface;
return XGR_ScreenSurface.get();
}

uint8_t* XGR_Screen::get_2d_render_buffer() {
return XGR_ScreenSurface2D;
return XGR_ScreenSurface2D.get();
}

uint32_t* XGR_Screen::get_2d_rgba_render_buffer() {
return XGR_ScreenSurface2DRgba;
return XGR_ScreenSurface2DRgba.get();
}

void XGR_Screen::set_active_render_buffer(uint8_t *buf) {
ScreenBuf = (unsigned char*)buf;
}

void XGR_Screen::set_default_render_buffer() {
set_active_render_buffer(XGR_ScreenSurface);
set_active_render_buffer(get_default_render_buffer());
}

void XGR_Screen::set_2d_render_buffer() {
set_active_render_buffer(XGR_ScreenSurface2D);
set_active_render_buffer(get_2d_render_buffer());
}

SDL_Surface* XGR_Screen::get_screenshot() {
Expand Down Expand Up @@ -947,7 +944,7 @@ void XGR_Screen::flip()
void *pixels;
int pitch;
SDL_LockTexture(sdlTexture, NULL, &pixels, &pitch);
blitRgba((uint32_t*)pixels, XGR_ScreenSurface, XGR_ScreenSurface2DRgba, XGR_ScreenSurface2D);
blitRgba((uint32_t*)pixels, get_default_render_buffer(), get_2d_rgba_render_buffer(), get_2d_render_buffer());
SDL_UnlockTexture(sdlTexture);

SDL_RenderClear(sdlRenderer);
Expand Down Expand Up @@ -1040,8 +1037,9 @@ void XGR_Screen::flush(int x,int y,int sx,int sy)

void XGR_Screen::fill(int col, void* buffer)
{
if (buffer == XGR_ScreenSurface2DRgba) {
memset(XGR_ScreenSurface2DRgba, col, ScreenX * ScreenY * 4);
auto surface2dRgba = XGR_ScreenSurface2DRgba.get();
if (buffer == surface2dRgba) {
memset(surface2dRgba, col, ScreenX * ScreenY * 4);
} else {
memset(buffer == NULL ? ScreenBuf : buffer, col, ScreenX * ScreenY);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/xgraph/xgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ struct XGR_Screen

uint8_t *ScreenBuf;

uint8_t *XGR_ScreenSurface;
uint8_t *XGR_ScreenSurface2D;
uint32_t *XGR_ScreenSurface2DRgba;
std::unique_ptr<uint8_t[]> XGR_ScreenSurface;
std::unique_ptr<uint8_t[]> XGR_ScreenSurface2D;
std::unique_ptr<uint32_t[]> XGR_ScreenSurface2DRgba;
SDL_Surface *XGR32_ScreenSurface;

SDL_Surface *IconSurface;
Expand Down
1 change: 1 addition & 0 deletions lib/xtool/xglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <memory>

#ifdef __WORDSIZE
#else
Expand Down
4 changes: 2 additions & 2 deletions src/actint/ascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ aciLocationShutterInfo* locSh;
aciWorldMap* wMap;
aciWorldInfo* wData;

actIntDispatcher* aScrDisp;
std::unique_ptr<actIntDispatcher> aScrDisp;

int aciCurCredits03 = 0;

Expand All @@ -605,7 +605,7 @@ void aParseScript(const char* fname,const char* bname)
invMatrix* mtx;
invItem* itm;

aScrDisp = new actIntDispatcher;
aScrDisp.reset(new actIntDispatcher);
aciML_D = new aciML_Dispatcher;

#ifndef _BINARY_SCRIPT_
Expand Down
1 change: 1 addition & 0 deletions src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <math.h>
#include <time.h>
#include <ctime>
#include <memory>

#if (defined(__unix__) || defined(__APPLE__))
#include <dirent.h>
Expand Down

0 comments on commit 95a0a65

Please sign in to comment.