File searching error

So i needed a function for my program that will find all the files in exe-location/Files. Since i have yet to delve into windows programming, i just found to differnt bits of code and splice them into my Frankenstein function here. the first bit gets the working directory of the exe, and then appends "//Files\\*" to it so the next section can return the name of all the files in that directory.

My question is about the char* "buffer", and whether or not i need to delete/free it from memory. when i put "delete buffer" at the end, it crashes the program right after i build it, but then when i run it again from the same build, it will no longer crash. not including the delete statement causes no problems, but i'm curious if that is memory leak.

any help as to why this occurs, whether or not leaving buffer creates a memory leak, or tips on a better way to achieve my desired result(which is storing strings containing all the file names in that folder) would help

Thanks :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void File_Loader::get_file_locs( )///stores the level file names
{
    char* buffer;

    if ( ( buffer = _getcwd( NULL, 0) ) == NULL )
    {
        std::cout << "Working directory/direct.h ERROR!\n";
    }


    //append levels folder to the directory
    strcat( buffer, "\\Files\\*" );
    #ifdef DEBUG_MEM
    std::cout << buffer << std::endl;
    #endif // DEBUG_MEM

    WIN32_FIND_DATA files;
    HANDLE search_handle = FindFirstFile( buffer , &files );
    if ( search_handle )
    {
        do
        {
            std::cout << files.cFileName << std::endl;
        }while( FindNextFile( search_handle, &files ) );

    }
    CloseHandle( search_handle );
 
    /** This is the part i have the question about **/
    delete buffer;


}


Edit: move to beginners because windows section is fairly dead and the main question isnt purely windows related
Last edited on
bump cause moving it buried it
The issue here is that the memory for buffer was allocated using malloc(), as stated in the documentation (not chiding you, just showing you): http://msdn.microsoft.com/en-us/library/sf98bd4y.aspx

Therefore, you have to call free() to release the memory. Calling delete on memory allocated with malloc() is actually undefined behavior, so the compiler can do whatever it wants, including crashing your program.

http://stackoverflow.com/questions/10854210/behaviour-of-malloc-with-delete-in-c
I should have mentioned that i have tried "free()". the result is around the same as delete. as in it crashes the program. so that wouldnt seem to be the main issue.

I just tried running the program with

delete: Seemingly random crashes

free(): Seemingly random crashes

no deletion: no crash

Alright! i spent some time commenting out various bits and found out that "strcat" was what was causing the deletion error.

i fixed the error by creating a string, copying the buffer to that, and appending the rest of the file path to that string. This works perfectly.

*side note, both free() & delete do not cause crashes now

new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    char* buffer;

    if ( ( buffer = _getcwd( NULL, 0) ) == NULL )
    {
        std::cout << "Working directory/direct.h ERROR!\n";
    }


    //append levels folder to the directory
    std::string work_dir( buffer );
    free(buffer);
    work_dir += "\\Files\\*";

    #ifdef DEBUG_MEM
    std::cout << work_dir << std::endl;
    #endif // DEBUG_MEM

    WIN32_FIND_DATA files;
    HANDLE search_handle = FindFirstFile( work_dir.c_str() , &files );
    if ( search_handle )
    {
        do
        {
            std::cout << files.cFileName << std::endl;
        }while( FindNextFile( search_handle, &files ) );

    }
    CloseHandle( search_handle );
Topic archived. No new replies allowed.