Skip to content

Commit

Permalink
Fix some valgrind warnings and a leak in openclonk-server
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaesar committed Sep 1, 2017
1 parent 2db1ae5 commit 94394c2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/graphics/C4FontLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class CStdFont
inline int GetLineHeight() const
{
#ifdef USE_CONSOLE
return 0;
return 1;
#else
return iLineHgt;
#endif
Expand Down
22 changes: 11 additions & 11 deletions src/graphics/C4Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ DWORD C4Surface::GetPixDw(int iX, int iY, bool fApplyModulation)
// get+lock affected texture
if (!texture) return 0;
texture->Lock();
pBuf=(BYTE *) texture->texLock.pBits;
pBuf=(BYTE *) texture->texLock.pBits.get();
iPitch=texture->texLock.Pitch;
}
// get pix of surface
Expand Down Expand Up @@ -593,7 +593,7 @@ bool C4Surface::BltPix(int iX, int iY, C4Surface *sfcSource, int iSrcX, int iSrc
// 32bit-blit. lock target
if (!texture) return false;
texture->Lock();
DWORD *pPix32 = (DWORD *)(((BYTE *)texture->texLock.pBits) + iY*texture->texLock.Pitch + iX * 4);
DWORD *pPix32 = (DWORD *)(((BYTE *)texture->texLock.pBits.get()) + iY*texture->texLock.Pitch + iX * 4);
// get source pix as dword
DWORD srcPix=sfcSource->GetPixDw(iSrcX, iSrcY, true);
// merge
Expand Down Expand Up @@ -640,7 +640,7 @@ C4TexRef::C4TexRef(int iSizeX, int iSizeY, int iFlags)
#ifndef USE_CONSOLE
texName = 0;
#endif
texLock.pBits=nullptr; fIntLock=false;
texLock.pBits.reset(); fIntLock=false;
// store size
this->iSizeX=iSizeX;
this->iSizeY=iSizeY;
Expand All @@ -657,9 +657,9 @@ C4TexRef::C4TexRef(int iSizeX, int iSizeY, int iFlags)

if ((iFlags & C4SF_Unlocked) == 0 && pDraw)
{
texLock.pBits = new unsigned char[iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES];
texLock.pBits = std::make_unique<unsigned char[]>(iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES);
texLock.Pitch = iSizeX * C4Draw::COLOR_DEPTH_BYTES;
memset(texLock.pBits, 0x00, texLock.Pitch*iSizeY);
memset(texLock.pBits.get(), 0x00, texLock.Pitch*iSizeY);
// Always locked
LockSize.x = LockSize.y = 0;
LockSize.Wdt = iSizeX; LockSize.Hgt = iSizeY;
Expand All @@ -673,7 +673,7 @@ C4TexRef::~C4TexRef()
#ifndef USE_CONSOLE
if (pGL && pGL->pCurrCtx) glDeleteTextures(1, &texName);
#endif
if (pDraw) delete [] static_cast<unsigned char*>(texLock.pBits); texLock.pBits = nullptr;
if (pDraw) texLock.pBits = nullptr;
// remove from texture manager
pTexMgr->UnregTex(this);
}
Expand Down Expand Up @@ -716,7 +716,7 @@ bool C4TexRef::LockForUpdate(C4Rect & rtUpdate)
// lock
#ifndef USE_CONSOLE
// prepare texture data
texLock.pBits = new unsigned char[rtUpdate.Wdt * rtUpdate.Hgt * C4Draw::COLOR_DEPTH_BYTES];
texLock.pBits = std::make_unique<unsigned char[]>(rtUpdate.Wdt * rtUpdate.Hgt * C4Draw::COLOR_DEPTH_BYTES);
texLock.Pitch = rtUpdate.Wdt * C4Draw::COLOR_DEPTH_BYTES;
LockSize = rtUpdate;
return true;
Expand All @@ -737,10 +737,10 @@ bool C4TexRef::Lock()
{
if (!pGL->pCurrCtx) return false;
// get texture
texLock.pBits = new unsigned char[iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES];
texLock.pBits = std::make_unique<unsigned char[]>(iSizeX * iSizeY * C4Draw::COLOR_DEPTH_BYTES);
texLock.Pitch = iSizeX * C4Draw::COLOR_DEPTH_BYTES;
glBindTexture(GL_TEXTURE_2D, texName);
glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits);
glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits.get());
return true;
}
#endif
Expand Down Expand Up @@ -772,9 +772,9 @@ void C4TexRef::Unlock()
glBindTexture(GL_TEXTURE_2D, texName);
glTexSubImage2D(GL_TEXTURE_2D, 0,
LockSize.x, LockSize.y, LockSize.Wdt, LockSize.Hgt,
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits);
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, texLock.pBits.get());

delete[] static_cast<unsigned char*>(texLock.pBits); texLock.pBits=nullptr;
texLock.pBits.reset();
if (fMipMap) glGenerateMipmap(GL_TEXTURE_2D);
#endif
}
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/C4Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ class C4Surface

typedef struct _LOCKED_RECT
{
int Pitch;
unsigned char * pBits;
int Pitch;
std::unique_ptr<unsigned char[]> pBits;
} LOCKED_RECT;

// one texture encapsulation
Expand Down Expand Up @@ -171,7 +171,7 @@ class C4TexRef
bool FillBlack(); // fill complete texture in black
void SetPix(int iX, int iY, DWORD v)
{
*((DWORD *)(((BYTE *)texLock.pBits) + (iY - LockSize.y) * texLock.Pitch + (iX - LockSize.x) * 4)) = v;
*((DWORD *)(((BYTE *)texLock.pBits.get()) + (iY - LockSize.y) * texLock.Pitch + (iX - LockSize.x) * 4)) = v;
}
private:
void CreateTexture();
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/C4SurfaceLoaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ bool C4Surface::ReadPNG(CStdStream &hGroup, int iFlags)
{
// Optimize the easy case of a png in the same format as the display
// 32 bit
DWORD *pPix=(DWORD *) (((char *) texture->texLock.pBits) + iY * texture->texLock.Pitch);
DWORD *pPix=(DWORD *) (((char *) texture->texLock.pBits.get()) + iY * texture->texLock.Pitch);
memcpy (pPix, png.GetRow(iY), maxX * sizeof(*pPix));
int iX = maxX;
while (iX--) { if (((BYTE *)pPix)[3] == 0x00) *pPix = 0x00000000; ++pPix; }
Expand All @@ -197,7 +197,7 @@ bool C4Surface::ReadPNG(CStdStream &hGroup, int iFlags)
// if color is fully transparent, ensure it's black
if (dwCol>>24 == 0x00) dwCol=0x00000000;
// set pix in surface
DWORD *pPix=(DWORD *) (((char *) texture->texLock.pBits) + iY * texture->texLock.Pitch + iX * 4);
DWORD *pPix=(DWORD *) (((char *) texture->texLock.pBits.get()) + iY * texture->texLock.Pitch + iX * 4);
*pPix=dwCol;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/C4MessageBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ C4MessageBoard::C4MessageBoard() : LogBuffer(C4LogSize, C4LogMaxLines, 0, " ",
iBackScroll = -1;
ScrollUpBinding = nullptr;
ScrollDownBinding = nullptr;
iLineHgt = 1; // Prevent unitialized access with USE_CONSOLE
}

C4MessageBoard::~C4MessageBoard()
Expand Down

0 comments on commit 94394c2

Please sign in to comment.