Skip to content

Commit

Permalink
PRINCE: Memory leaks fix - showBackAnim(), printAt(), checkMob()"
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslw committed Jun 29, 2014
1 parent 7ebcbea commit 191b4cc
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
2 changes: 2 additions & 0 deletions engines/prince/graphics.cpp
Expand Up @@ -46,6 +46,8 @@ GraphicsMan::GraphicsMan(PrinceEngine *vm)
GraphicsMan::~GraphicsMan() {
_frontScreen->free();
delete _frontScreen;
_screenForInventory->free();
delete _screenForInventory;
delete[] _shadowTable70;
delete[] _shadowTable50;
}
Expand Down
26 changes: 18 additions & 8 deletions engines/prince/prince.cpp
Expand Up @@ -140,6 +140,8 @@ PrinceEngine::~PrinceEngine() {
}
_maskList.clear();

freeDrawNodes();

clearBackAnimList();

for (uint i = 0; i < _allInvList.size(); i++) {
Expand Down Expand Up @@ -404,6 +406,8 @@ bool PrinceEngine::loadLocation(uint16 locationNr) {
_mobList[i]._visible = _script->getMobVisible(i);
}

freeDrawNodes();

clearBackAnimList();
_script->installBackAnims(_backAnimList, _room->_backAnim);

Expand Down Expand Up @@ -855,8 +859,10 @@ int PrinceEngine::checkMob(Graphics::Surface *screen, Common::Array<Mob> &mobLis
int phase = backAnim._showFrame;
int phaseFrameIndex = backAnim._animData->getPhaseFrameIndex(phase);
Graphics::Surface *backAnimSurface = backAnim._animData->getFrame(phaseFrameIndex);
byte *pixel = (byte *)backAnimSurface->getBasePtr(mousePosCamera.x - backAnim._currX, mousePosCamera.y - backAnim._currY);
if (*pixel != 255) {
byte pixel = *(byte *)backAnimSurface->getBasePtr(mousePosCamera.x - backAnim._currX, mousePosCamera.y - backAnim._currY);
backAnimSurface->free();
delete backAnimSurface;
if (pixel != 255) {
break;
}
}
Expand Down Expand Up @@ -923,13 +929,11 @@ int PrinceEngine::checkMob(Graphics::Surface *screen, Common::Array<Mob> &mobLis
return -1;
}

void PrinceEngine::printAt(uint32 slot, uint8 color, const char *s, uint16 x, uint16 y) {
void PrinceEngine::printAt(uint32 slot, uint8 color, char *s, uint16 x, uint16 y) {

debugC(1, DebugChannel::kEngine, "PrinceEngine::printAt slot %d, color %d, x %02d, y %02d, str %s", slot, color, x, y, s);

char *destStr = (char *)malloc(strlen(s));
strcpy(destStr, s);
char *strPointer = destStr;
char *strPointer = s;

if (getLanguage() == Common::DE_DEU) {
while (*strPointer) {
Expand Down Expand Up @@ -961,7 +965,7 @@ void PrinceEngine::printAt(uint32 slot, uint8 color, const char *s, uint16 x, ui
}

Text &text = _textSlots[slot];
text._str = destStr;
text._str = s;
text._x = x;
text._y = y;
text._color = color;
Expand Down Expand Up @@ -1140,6 +1144,9 @@ void PrinceEngine::showSprite(Graphics::Surface *spriteSurface, int destX, int d
newDrawNode.freeSurfaceSMemory = freeSurfaceMemory;
newDrawNode.drawFunction = &_graph->drawTransparentDrawNode;
_drawNodeList.push_back(newDrawNode);
} else if (freeSurfaceMemory) {
spriteSurface->free();
delete spriteSurface;
}
}

Expand All @@ -1159,6 +1166,9 @@ void PrinceEngine::showSpriteShadow(Graphics::Surface *shadowSurface, int destX,
newDrawNode.freeSurfaceSMemory = freeSurfaceMemory;
newDrawNode.drawFunction = &_graph->drawAsShadowDrawNode;
_drawNodeList.push_back(newDrawNode);
} else if (freeSurfaceMemory) {
shadowSurface->free();
delete shadowSurface;
}
}

Expand Down Expand Up @@ -1906,7 +1916,7 @@ void PrinceEngine::inventoryLeftMouseButton() {
int invObjExamEvent = _script->scanMobEvents(_invMobList[_selectedMob]._mask, _script->_scriptInfo.invObjExam);
if (invObjExamEvent == -1) {
// do_standard
printAt(0, 216, _invMobList[_selectedMob]._examText.c_str(), kNormalWidth / 2, _invExamY);
printAt(0, 216, (char *)_invMobList[_selectedMob]._examText.c_str(), kNormalWidth / 2, _invExamY);
_interpreter->setCurrentString(_invMobList[_selectedMob]._mask + 70000);
setVoice(0, 28, 1);
playSample(28, 0);
Expand Down
2 changes: 1 addition & 1 deletion engines/prince/prince.h
Expand Up @@ -250,7 +250,7 @@ class PrinceEngine : public Engine {
virtual GUI::Debugger *getDebugger();

void changeCursor(uint16 curId);
void printAt(uint32 slot, uint8 color, const char *s, uint16 x, uint16 y);
void printAt(uint32 slot, uint8 color, char *s, uint16 x, uint16 y);
int calcText(const char *s);

static const uint8 MAXTEXTS = 32;
Expand Down
8 changes: 4 additions & 4 deletions engines/prince/script.cpp
Expand Up @@ -462,7 +462,7 @@ void Interpreter::debugInterpreter(const char *s, ...) {
Common::String str = Common::String::format("@0x%08X: ", _lastInstruction);
str += Common::String::format("op %04d: ", _lastOpcode);
//debugC(10, DebugChannel::kScript, "PrinceEngine::Script %s %s", str.c_str(), buf);
if (_mode == "fg") {
if (!strcmp(_mode, "fg")) {
debug(10, "PrinceEngine::Script %s %s", str.c_str(), buf);
}
//debug("Prince::Script frame %08ld mode %s %s %s", _vm->_frameNr, _mode, str.c_str(), buf);
Expand Down Expand Up @@ -834,7 +834,7 @@ void Interpreter::O_SETSTRING() {
_currentString = offset;

if (offset >= 80000) {
_string = (const byte *)_vm->_variaTxt->getString(offset - 80000);
_string = (byte *)_vm->_variaTxt->getString(offset - 80000);
debugInterpreter("GetVaria %s", _string);
}
else if (offset < 2000) {
Expand Down Expand Up @@ -911,7 +911,7 @@ void Interpreter::O_XORFLAG() {
void Interpreter::O_GETMOBTEXT() {
uint16 mob = readScriptFlagValue();
_currentString = _vm->_locationNr * 100 + mob + 60001;
_string = (const byte *)_vm->_mobList[mob]._examText.c_str();
_string = (byte *)_vm->_mobList[mob]._examText.c_str();

debugInterpreter("O_GETMOBTEXT mob %d", mob);
}
Expand Down Expand Up @@ -1239,7 +1239,7 @@ void Interpreter::O_PRINTAT() {

uint8 color = _flags->getFlagValue(Flags::KOLOR);

_vm->printAt(slot, color, (const char *)_string, fr1, fr2);
_vm->printAt(slot, color, (char *)_string, fr1, fr2);

increaseString();
}
Expand Down
2 changes: 1 addition & 1 deletion engines/prince/script.h
Expand Up @@ -209,7 +209,7 @@ class Interpreter {
//uint8 _savedStacktop;
uint32 _waitFlag;

const byte *_string;
byte *_string;
uint32 _currentString;
const char *_mode;

Expand Down
4 changes: 2 additions & 2 deletions engines/prince/variatxt.cpp
Expand Up @@ -42,12 +42,12 @@ bool VariaTxt::loadFromStream(Common::SeekableReadStream &stream) {
return true;
}

const char *VariaTxt::getString(uint32 stringId) {
char *VariaTxt::getString(uint32 stringId) {
uint32 stringOffset = READ_LE_UINT32(_data + stringId * 4);
if (stringOffset > _dataSize) {
assert(false);
}
return (const char *)_data + stringOffset;
return (char *)_data + stringOffset;
}

}
Expand Down
2 changes: 1 addition & 1 deletion engines/prince/variatxt.h
Expand Up @@ -32,7 +32,7 @@ class VariaTxt {

bool loadFromStream(Common::SeekableReadStream &stream);

const char *getString(uint32 stringId);
char *getString(uint32 stringId);

private:
uint32 _dataSize;
Expand Down

0 comments on commit 191b4cc

Please sign in to comment.