For c is there a better debugger that could help track down potential memory leaks?

I feel like I have shored up loose ends, but CLion keeps saying I have a memory leak possible from main attached to the “files” var.

That being said, I am curious is there a util or toolset that will help me narrow down any potential issues like this better than the stock toolsets? That is the primary question! New to C coming from py and go. These are not issue way up on those high level langs… lol

example code below and an example of the amazingly help warning clion gives.

“Allocated memory is leaked when called from function ‘main'”
“Leak of memory allocated in function ‘get_files'”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <commdlg.h>
#include "log.h"
char** get_files(int* file_count) {
OPENFILENAME ofn;
char* szFile = malloc(8192 * sizeof(char));
char** files = NULL;
*file_count = 0;
if (szFile == NULL) {
return NULL;
}
// Initialize OPENFILENAME
szFile[0] = '';
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szFile;
ofn.nMaxFile = 8192;
ofn.lpstrFilter = "All Files*.*Text Files*.TXTCSV Files*.CSVDAT Files*.DAT";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER;
if (GetOpenFileName(&ofn) == TRUE) {
char* ptr = ofn.lpstrFile;
if (*(ptr + strlen(ptr) + 1) == '') {
// Single file case
*file_count = 1;
files = malloc(2 * sizeof(char*));
if (files == NULL) {
free(szFile);
return NULL;
}
files[0] = strdup(ptr);
if (files[0] == NULL) {
free(files);
free(szFile);
return NULL;
}
files[1] = NULL;
} else {
char* dir = ptr;
ptr += strlen(ptr) + 1;
int count = 0;
while (*ptr) {
count++;
ptr += strlen(ptr) + 1;
}
*file_count = count;
files = malloc((count + 1) * sizeof(char*));
if (files == NULL) {
free(szFile);
return NULL;
}
ptr = ofn.lpstrFile + strlen(ofn.lpstrFile) + 1;
for (int i = 0; i < count; i++) {
files[i] = malloc(strlen(dir) + strlen(ptr) + 2);
if (files[i] == NULL) {
for (int j = 0; j < i; j++) {
free(files[j]);
}
free(files);
free(szFile);
return NULL;
}
sprintf(files[i], "%s\%s", dir, ptr);
ptr += strlen(ptr) + 1;
}
files[count] = NULL;
}
}
free(szFile);
return files;
}
int main(void) {
Info(__func__, "Starting file validation routine");
int file_count = 0;
char** files = get_files(&file_count);
if (files == NULL) {
Info(__func__, "Getting the files to process failed");
return 1;
}
for (int i = 0; i < file_count; i++) {
char* shortName = strrchr(files[i], '\');
if (shortName != NULL) {
shortName++;
const size_t bufferSize = strlen("Validating: ") + strlen(shortName) + 1;
char* message = malloc(bufferSize);
if (message != NULL) {
snprintf(message, bufferSize, "Validating: %s", shortName);
Info(__func__, message);
free(message);
}
}
free(files[i]);
}
free(files);
return 0;
}
</code>
<code>#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <commdlg.h> #include "log.h" char** get_files(int* file_count) { OPENFILENAME ofn; char* szFile = malloc(8192 * sizeof(char)); char** files = NULL; *file_count = 0; if (szFile == NULL) { return NULL; } // Initialize OPENFILENAME szFile[0] = ''; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = NULL; ofn.lpstrFile = szFile; ofn.nMaxFile = 8192; ofn.lpstrFilter = "All Files*.*Text Files*.TXTCSV Files*.CSVDAT Files*.DAT"; ofn.nFilterIndex = 1; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = NULL; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER; if (GetOpenFileName(&ofn) == TRUE) { char* ptr = ofn.lpstrFile; if (*(ptr + strlen(ptr) + 1) == '') { // Single file case *file_count = 1; files = malloc(2 * sizeof(char*)); if (files == NULL) { free(szFile); return NULL; } files[0] = strdup(ptr); if (files[0] == NULL) { free(files); free(szFile); return NULL; } files[1] = NULL; } else { char* dir = ptr; ptr += strlen(ptr) + 1; int count = 0; while (*ptr) { count++; ptr += strlen(ptr) + 1; } *file_count = count; files = malloc((count + 1) * sizeof(char*)); if (files == NULL) { free(szFile); return NULL; } ptr = ofn.lpstrFile + strlen(ofn.lpstrFile) + 1; for (int i = 0; i < count; i++) { files[i] = malloc(strlen(dir) + strlen(ptr) + 2); if (files[i] == NULL) { for (int j = 0; j < i; j++) { free(files[j]); } free(files); free(szFile); return NULL; } sprintf(files[i], "%s\%s", dir, ptr); ptr += strlen(ptr) + 1; } files[count] = NULL; } } free(szFile); return files; } int main(void) { Info(__func__, "Starting file validation routine"); int file_count = 0; char** files = get_files(&file_count); if (files == NULL) { Info(__func__, "Getting the files to process failed"); return 1; } for (int i = 0; i < file_count; i++) { char* shortName = strrchr(files[i], '\'); if (shortName != NULL) { shortName++; const size_t bufferSize = strlen("Validating: ") + strlen(shortName) + 1; char* message = malloc(bufferSize); if (message != NULL) { snprintf(message, bufferSize, "Validating: %s", shortName); Info(__func__, message); free(message); } } free(files[i]); } free(files); return 0; } </code>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <commdlg.h>
#include "log.h"

char** get_files(int* file_count) {
    OPENFILENAME ofn;
    char* szFile = malloc(8192 * sizeof(char));
    char** files = NULL;
    *file_count = 0;
    if (szFile == NULL) {
        return NULL;
    }

    // Initialize OPENFILENAME
    szFile[0] = '';
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = NULL;
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = 8192;
    ofn.lpstrFilter = "All Files*.*Text Files*.TXTCSV Files*.CSVDAT Files*.DAT";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER;

    if (GetOpenFileName(&ofn) == TRUE) {
        char* ptr = ofn.lpstrFile;
        if (*(ptr + strlen(ptr) + 1) == '') {
            // Single file case
            *file_count = 1;
            files = malloc(2 * sizeof(char*));
            if (files == NULL) {
                free(szFile);
                return NULL;
            }
            files[0] = strdup(ptr);
            if (files[0] == NULL) {
                free(files);
                free(szFile);
                return NULL;
            }
            files[1] = NULL;
        } else {
            char* dir = ptr;
            ptr += strlen(ptr) + 1;
            int count = 0;
            while (*ptr) {
                count++;
                ptr += strlen(ptr) + 1;
            }
            *file_count = count;
            files = malloc((count + 1) * sizeof(char*));
            if (files == NULL) {
                free(szFile);
                return NULL;
            }
            ptr = ofn.lpstrFile + strlen(ofn.lpstrFile) + 1;
            for (int i = 0; i < count; i++) {
                files[i] = malloc(strlen(dir) + strlen(ptr) + 2);
                if (files[i] == NULL) {
                    for (int j = 0; j < i; j++) {
                        free(files[j]);
                    }
                    free(files);
                    free(szFile);
                    return NULL;
                }
                sprintf(files[i], "%s\%s", dir, ptr);
                ptr += strlen(ptr) + 1;
            }
            files[count] = NULL;
        }
    }
    free(szFile);
    return files;
}

int main(void) {
    Info(__func__, "Starting file validation routine");
    int file_count = 0;
    char** files = get_files(&file_count);
    if (files == NULL) {
        Info(__func__, "Getting the files to process failed");
        return 1;
    }
    for (int i = 0; i < file_count; i++) {
        char* shortName = strrchr(files[i], '\');
        if (shortName != NULL) {
            shortName++;
            const size_t bufferSize = strlen("Validating: ") + strlen(shortName) + 1;
            char* message = malloc(bufferSize);
            if (message != NULL) {
                snprintf(message, bufferSize, "Validating: %s", shortName);
                Info(__func__, message);
                free(message);
            }
        }
        free(files[i]);
    }
    free(files);
    return 0;
}

While helping debug my crap code is not part of the question, if you can see where I was dumb and want to help, feel free… lol

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật