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 6333459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:27:57+00:00 2026-05-24T18:27:57+00:00

I run createNewBoard which calls createNewMatrix and the I exit the program, and I

  • 0

I run createNewBoard which calls createNewMatrix and the I exit the program, and I have a memory leak I can’t find. Here’s the code

BoardP createNewBoard(int width, int high)
{
    BoardP board = (BoardP) malloc(sizeof(Board));

    if (board == NULL)
    {
        reportError(MEM_OUT);
        return NULL;
    }
    board->height = high;
    board->width = width;
    board->matrix = createNewMatrix(width,high);
    printf("%c",board->matrix[1][1]);
    if (board->matrix == NULL)
    {
        reportError(MEM_OUT);
        freeBoard(board);
    return NULL;
    }
return board;
}

static char** createNewMatrix(int width, int height){
    char** newMatrix = (char**) calloc(height,sizeof(char*));
    int i;
    for (i=0; i<height; i++)
    {
        newMatrix[i] = (char*) calloc(width,sizeof(char)); //LINE 71
        if (newMatrix[i] == NULL)
        {
            int j;
            for (j=0; j<i; j++)
            {
                free(newMatrix[j]);
            }
            free(newMatrix);
            return NULL;
        }
    }
    return newMatrix;
 }

It is driving me crazy. All I do is to create a pointer to Board struct (which holds to integers and a two dimensional pointer array) and I have a memory leak. Here’s the message:

==10436== HEAP SUMMARY:
==10436==     in use at exit: 100 bytes in 10 blocks
==10436==   total heap usage: 12 allocs, 2 frees, 196 bytes allocated
==10436== 
==10436== 100 bytes in 10 blocks are definitely lost in loss record 1 of 1
==10436==    at 0x4C2380C: calloc (vg_replace_malloc.c:467)
==10436==    by 0x4008C6: createNewMatrix (Board.c:71)
==10436==    by 0x40081E: createNewBoard (Board.c:55)
==10436==    by 0x4007C6: createNewDefaultBoard (Board.c:37)
==10436==    by 0x400F0C: main (PlayBoard.c:11)
==10436== 
==10436== LEAK SUMMARY:
==10436==    definitely lost: 100 bytes in 10 blocks
==10436==    indirectly lost: 0 bytes in 0 blocks
==10436==      possibly lost: 0 bytes in 0 blocks
==10436==    still reachable: 0 bytes in 0 blocks
==10436==         suppressed: 0 bytes in 0 blocks

It points me to line 71, which calls calloc for a line in the matrix. When the program exits it calls freeBoard:

void freeBoard(BoardP board)
{
    if (board != NULL)
    {
        if(board->matrix != NULL)
        {
            free(board->matrix);
        }
        free(board);
    }
}

Any ideas why I have a memory leak? Thanks!

  • 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-05-24T18:27:58+00:00Added an answer on May 24, 2026 at 6:27 pm

    You have to free the individual lines, before freeing matrix.

    for (i=0; i<height; i++)
    {
        free(board->matrix[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I run this code: graphic.addAnimation("standart",new int[] {0,1},1.0,true); which calls the graphic.addAnimation(String,int[],float,boolean) Method: public void
I run a script which calls another script through a ssh connection. My script
I run the code gives me the following sample data md5deep find * |
Run time error Cannot find column 0. below is my code string connectiostring =
I run a game and the running is done by hand, I have a
Run the following Java code: boolean b = false; Double d1 = 0d; Double
I run my sites all on InnoDB tables which is working really well so
I run the following code StringBuilder sb = new StringBuilder(); Stack stack = new
I run this code using both gpp and microsoft compiler but in both case
I run some validation on my form when a checkbox is clicked, which works

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.