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

  • Home
  • SEARCH
  • 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 6702927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:06:39+00:00 2026-05-26T07:06:39+00:00

I am getting the following error when running my program. Debug Assertion Failed! File:

  • 0

I am getting the following error when running my program.

Debug Assertion Failed!
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1322
Expression: _CrtIsValidHeapPointer(pUserData)

So I debugged my program and found out that the problem occurred when the following function was called the second time, more specifically the free statement at the end.

int writeToLog(char* str, enum LOGLEVEL logLevel) {
    if(logFile && logLevel >= level) {
        FILE* log;
        char *now = (char *)malloc(sizeof(char)*1024);
        time_t timer = time(NULL);
        if(*now == NULL) {
            return -1;
        }       
        now = ctime(&timer);
        if(now[strlen(now) - 1] == '\n') {
            now[strlen(now) - 1] = '\0';
        }
        log = fopen(logFile, "a+");
        if (log == NULL)
            return -1;
        fprintf(log, "%s%s\n", now, str);
        fclose(log);
        free(now); //fails here on the second function call
    }
    return 0;
}

Now I would love to just make now a constant char array but visual studio isn’t letting me do that because of the return type of ctime. Can anyone help?
Cheers.

  • 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-26T07:06:40+00:00Added an answer on May 26, 2026 at 7:06 am

    You are replacing the pointer now with a different one returned by ctime. Then you are trying to free it. So you end up freeing the pointer returned by ctime, and not the pointer you allocated yourself.

    You are not supposed to modify the pointer returned by ctime.

    For your purposes, you don’t even need to allocate any memory at all. You can just use the pointer returned by ctime directly.

    So this should work just fine:

    int writeToLog(char* str, enum LOGLEVEL logLevel) {
        if(logFile && logLevel >= level) {
            FILE* log;
            time_t timer = time(NULL);
    
            const char *now = ctime(&timer);
    
            size_t length = strlen(now);
            if(now[length - 1] == '\n') {
                now[length - 1] = '\0';
            }
            log = fopen(logFile, "a+");
            if (log == NULL)
                return -1;
            fprintf(log, "%s%s\n", now, str);
            fclose(log);
        }
        return 0;
    }
    

    Also note that you make two calls to strlen(now). You should call it once and save the result.

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

Sidebar

Related Questions

I am getting the following error when running my program. Compilation works just fine!
I am getting the following error when I try running my program in the
I am getting the following error while running my MVC application, which uses the
I'm getting the following error when running warble under both jRuby 1.3 and 1.4.
I'm running Valgrind and I'm getting the following error (this is not the only
I'm running Visual Studio 2008 SP1, and I'm getting the following error when trying
I'm getting the error message when running the following code from a C# console
I'm running Visual Studio 2005 Pro, and have been getting the following error recently:
Im getting following error while running the query. org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found 'LIMIT' near
I am getting this error while running a hadoop pipes program. The program compiles

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.