Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

couple of surface/script mem leak fixes proposal. #586

Merged
merged 1 commit into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions lib/xgraph/xgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ int XGR_Screen::init(int x,int y,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 @@ -293,9 +293,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 @@ -867,27 +864,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 @@ -930,7 +927,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 @@ -1023,8 +1020,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 @@ -195,9 +195,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