skip to Main Content

I’m having some problems trying to free a buffer created in a function from outside that function in C. What I’m doing is to create bufA, do some stuff with it, then call a function which creates bufB, whose length is calculated inside that function. Bytes in bufA are then copied to bufB with some additional bytes added, then the function returns. Some further processing is done with bufB back in main(), then both bufA and bufB are freed. The program runs correctly, but throws an error when free(bufB) is called.

For very much simplified code, I have something like this:

typedef unsigned char BYTE;

// -----

int main(void) {

  // -----

  BYTE bufA = NULL;
  // Specify lenBufA
  bufA = calloc(lenBufA, 1);
  // Populate bufA with some stuff.
  BYTE bufB = NULL;
  int lenBufB = myFunction(bufA, &bufB);
  // Do something with bufB.
  free(bufA);
  free(bufB); // <=== Throws an error here.
  return 0;
}

Then in myFunction() I may have something like this:

int myFunction(BYTE* inBuf, BYTE** outBuf) {
  // Initialization.
  // Calculate the length that outBuf will have and put it in outLen.
  *outBuf = (BYTE*)calloc(outLen, 1);
  // Do stuff with outBuf.
  return outLen;
}

inBuf is not specified as constant as some bytes may be changed. calloc() rather than malloc() is used in main() and in the function because I want the bytes in both buffers to be initialized to zero, as a few bytes in both are not given values.

I’m using visual Studio 2022 with Windows 10. Somehow the error caused by free(bufB) must be related to the fact that memory is allocated in the function, not in main(). How is this fixed?

2

Answers


  1. Chosen as BEST ANSWER

    On Googling ntdll.dll, it appears to be a Windows file of some type, and based on some blurb, it might be corrupted.

    Accordingly, I copied the source files to another Windows 10 computer that also has Visual Studio 2022 installed, created a project, then compiled the source code. On executing I get the same message with different hex codes, However the hex code for sixbit-encoding6.exe is the same. Unfortunately the copy and paste feature of the mouse doesn't work for the message on that computer, but it thus looks as if the dll is probably not corrupted, and there is a problem with my application.


  2. Your code is incomplete and does not compile. The main issue appears to be that the type of bufA and bufB is BYTE but should be a pointer BYTE *. Here is working code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef unsigned char BYTE;
    
    int myFunction(const BYTE *inBuf, BYTE **outBuf) {
        int outLen = strlen(inBuf);
        *outBuf = calloc(outLen, 1);
        if(!*outBuf) {
            printf("calloc failedn");
            exit(1);
        }
        return outLen;
    }
    
    int main(void) {
        BYTE *bufA = NULL;
        int lenBufA = 42;
        bufA = calloc(lenBufA, 1);
        if(!bufA) {
            printf("calloc failedn");
            exit(1);
        }
        memset(bufA, !'', lenBufA - 1); // heh
    
        BYTE *bufB = NULL;
        int lenBufB = myFunction(bufA, &bufB);
        free(bufA);
        free(bufB);
        return 0;
    }
    

    It checks the return code of calloc, clarifies with const BYTE *inBuf that function doesn’t change that argument.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search