Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:20:53+00:00 2026-06-13T21:20:53+00:00

I have another valgrind error that I can’t seem to figure out. I have

  • 0

I have another valgrind error that I can’t seem to figure out. I have some code that loads a script then uses strtok() to tokenize the data in said script that has been loaded into a buffer. I allocate memory on the heap for my buffer using the following:

char *pPngStr = new char[4096];

Then at the end of the function, I free the memory using:

delete[] pPngStr;

Valgrind reports the allocation as so:

173 bytes in 1 blocks are definitely lost in loss record 3,753 of 4,627

Valgrind also reports the line where I free the memory as:

Invalid free() / delete / delete[] / realloc()

This is all in the same function is the variable is local to only that function and not the class. I am at a loss as to how valgrind thinks that this is a memory leak. I am allocating all the memory I need then I am freeing it when I am done. I don’t see what the problem is.

Here is the complete function (it may be a little messy):

void CSprite::LoadSprite()
{
FILE *pF = fopen("Data/Art/coin/coin.sprite", "rb");

if(!pF)
{
    return;
}

fseek(pF, 0, SEEK_END);
int length = ftell(pF);
fseek(pF, 0, SEEK_SET);

char *aData = new char[length];

fread(aData, 1, length, pF);

CheckGLError();

/**
  * The scripts are setup like so:
  ***********************************************
  * Pngs:
  * 4; // This is the number of frames.
  * <pngname>_001.png;
  * <pngname>_002.png;
  * <pngname>_003.png;
  * <pngname>_004.png;
  *
  * Properties:
  * name=<obj name>;
  * width=###;
  * height=###;
  * framerate=0.### // rate in seconds
  ************************************************
  */

mpSprite = new SGfxSprite;

char *pPngStr = new char[4096];
pPngStr = strtok(aData, "\n");

pPngStr = strtok(NULL, "\n");
int iNumFrames = atoi(pPngStr);
mpSprite->numFrames = iNumFrames;

mpSprite->textures = new GLuint[iNumFrames];
glGenTextures(iNumFrames, mpSprite->textures);

int i = 0; // used for loop only!
while(pPngStr != NULL)
{
    pPngStr = strtok(NULL, "\n");

    if(!strcmp(pPngStr, "Properties:") || i >= iNumFrames)
    {
        break;
    }

    if(mpPng->LoadPng(pPngStr))
    {
        CheckGLError();

        glBindTexture(GL_TEXTURE_2D, mpSprite->textures[i]);
        CheckGLError();

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        CheckGLError();

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        CheckGLError();

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        CheckGLError();

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        CheckGLError();

        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
        CheckGLError();

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mpPng->miWidth, mpPng->miHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mpPng->mpData);
        CheckGLError();
    }

    ++i;
}

pPngStr = strtok(NULL, "=\n");
pPngStr = strtok(NULL, "\n");
mpSprite->name = pPngStr;

pPngStr = strtok(NULL, "=\n");
pPngStr = strtok(NULL, "\n");
mpSprite->width = atoi(pPngStr);

pPngStr = strtok(NULL, "=\n");
pPngStr = strtok(NULL, "\n");
mpSprite->height = atoi(pPngStr);

pPngStr = strtok(NULL, "=\n");
pPngStr = strtok(NULL, "\n");
mpSprite->framerate = (atof(pPngStr) * 1000);

mpSprite->x = 0;
mpSprite->y = 0;

mpSprite->currentFrame = 0;
mpSprite->paintTimer = 0;

fclose(pF);

delete[] pPngStr, aData;

}

*PLEASE NOTE: this function contains OpenGL code and also invokes members from data structures that are not defined here. The code also compiles without any warnings or errors in Xubuntu 12.04 with g++ (gcc 4.6) using Qt Creator 2.4.1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:20:55+00:00Added an answer on June 13, 2026 at 9:20 pm
    char *pPngStr = new char[4096];
    pPngStr = strtok(aData, "\n");
    

    Just in the line after the new, you are forgetting about the pointer that you go and using the variable to store other data. No doubt that there is a memory leak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another script that I can't figure out what is wrong with it.
I have another problem that I can't solve I have a following code that
i have another question on getting my XML serialization neat, which i can't seem
I have another beginner's question that hopefully someone can help with. I'm trying to
I have another strange bug which I cannot figure out. I try to create
I have another newbie Python question. I have the following piece of code that
I have another sqlite script that I need to convert to postgres. Take a
I have another one for you guys. I've searched around but I can't figure
I have another web app, that uses the liferay user database. But before a
I have another programmer who I'm trying to explain why it is that a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.