_CrtDumpMemoryLeaks 无法在 c、Visual Studio 2022 中工作

问题描述 投票:0回答:1

我正在使用 Visual Studio 2022。

出于某种原因?

_CrtDumpMemoryLeaks();
没有显示任何泄漏,仅以 0 退出。

尽管我做了 malloc char*leakyMemory 进行测试,但无论如何都没有帮助。

一切正常,我只是想检查内存泄漏。

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> // For Sleep()
// Include your store header file
#include "Store.h"
#include "Customer.h"
#define TEXT_FILE "store2.txt"
#define BIN_FILE "store.bin"

int main() {

    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    Store myStore;
    
    char* leakyMemory = (char*)malloc(128);
    leakyMemory = "ssssss";
   
    int roleChoice = 0, actionChoice = 0;;

    //// Initialize the store
    if (!initStoreFromFiles(&myStore)) {
        printf("Failed to initialize the store.\n");
        //exit(0);
    }
   /*initStore(&myStore);*/
    do {
        printf("\nAre you a customer or an employee?\n");
        printf("1. Customer\n");
        printf("2. Employee\n");
        printf("3. Developer Menu(to see all options)\n");
        printf("0. Exit and save\n");
        scanf("%d", &roleChoice);

        switch (roleChoice) {
        case 1: // Customer
            showCustomerMenu(&myStore);
            break;

        case 2: // Employee
            showEmployeeMenu(&myStore);
            break;
        case 3: // Developer 
            showDeveloperMenu(&myStore);
            break;

        case 0: // Exit
            
            printf("Exiting...\n");
            saveStoreToFile(&myStore, TEXT_FILE);
            saveStoreToBinFile(&myStore, BIN_FILE);
            
            
            break;

        default:
            printf("Invalid role. Please try again.\n");
        }
    } while (roleChoice != 0);

    freeStore(&myStore);
    _CrtDumpMemoryLeaks();
    
    return 0;
   
}
c visual-studio memory-leaks
1个回答
0
投票

你想要这个:

#define _CRTDBG_MAP_ALLOC

#include <stdio.h>
#include <crtdbg.h>

int main()
{
  _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  char *a = malloc(199);

  _CrtDumpMemoryLeaks();
  return 0;
}

© www.soinside.com 2019 - 2024. All rights reserved.