CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 42
  1. #1
    Join Date
    Nov 2003
    Posts
    19

    Debug Assertion Failed on free pointer

    Hi,
    Currently I am using a char* pointer to point to a file.
    The program goes like this:-

    char* file[] = {"test1.txt", "test2.txt"};

    void process()
    {
    char* ptr;
    whie(1)
    {
    ptr = file[i];
    parse(ptr);
    free(ptr);
    ptr = NULL;
    remove(file[i]);
    }
    }

    I am getting debug assertion failed error message.
    It says :-
    Filebgheap.c
    Line:1044
    expression:_CrtIsValidHeapPointer(pUserData)

    Everytime it goes to free(ptr), I get this message. How am I going to get rid of this?

    Please help.

    Thank You.

  2. #2
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537

    Re: Debug Assertion Failed on free pointer

    Originally posted by nhcheng
    Hi,
    Currently I am using a char* pointer to point to a file.
    The program goes like this:-

    char* file[] = {"test1.txt", "test2.txt"};

    void process()
    {
    char* ptr;
    whie(1)
    {
    ptr = file[i];
    parse(ptr);
    free(ptr);
    ptr = NULL;
    remove(file[i]);
    }
    }

    I am getting debug assertion failed error message.
    It says :-
    Filebgheap.c
    Line:1044
    expression:_CrtIsValidHeapPointer(pUserData)

    Everytime it goes to free(ptr), I get this message. How am I going to get rid of this?

    Please help.

    Thank You.
    where to begin....

    You don't free/delete anything unless you have explicitly allocated it. And the code you posted, does not show it.

    free is used with malloc
    delete is used with new

    Look them up on msdn.microsoft.com

  3. #3
    Join Date
    Nov 2003
    Posts
    19
    hi,
    Actually my main concern is to remove the files from my disk once the parsing process is done.

    I thought I have to free the pointer which is pointing to the files before remove the files.

    What I encounter is that although I use remove(file); the file is still in my disk. I tried to delete it manually but access denied as the file is being used. So I was suspecting it is because of the char* pointer.

    Do you have any suggestion to remove the files permanently?


    Thank You.

  4. #4
    Join Date
    Dec 2003
    Posts
    220

     

    Originally posted by nhcheng
    hi,
    Actually my main concern is to remove the files from my disk once the parsing process is done.

    I thought I have to free the pointer which is pointing to the files before remove the files.

    What I encounter is that although I use remove(file); the file is still in my disk. I tried to delete it manually but access denied as the file is being used. So I was suspecting it is because of the char* pointer.

    Do you have any suggestion to remove the files permanently?


    Thank You.
    To delete a file in a directory, I think this question iわs answered not quite long ago right on this VC++ forum, if you dont care to do a search though...

    Regards,
    homestead

  5. #5
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    How are you removing the files? How are you opening the files? There are a number of API's to delete files such as:

    DeleteFile(...) WIN32
    OpenFile(...) WIN32 (passing delete of course) (also a depreciated function)
    remove(...) CRT

    etc...

    if there is an exclusive lock on the file, then you will not be able to remove it until that process/thread whatever have you closes the handle on the file.

  6. #6
    Join Date
    Dec 2003
    Posts
    220

    Here the code

    PHP Code:
    BOOL DeleteFile(const char*_strRootDirectory,const char_filename)
    {
    bool bSubdirectory false;

    HANDLE hFile;
    string strFilePath;
    string strPattern;
    WIN32_FIND_DATA FileInformation;
    string strRootDirectory=_strRootDirectory;


    strPattern strRootDirectory "\\*.*";
    hFile FindFirstFile(strPattern.c_str(), &FileInformation);
    FindNextFile(hFile, &FileInformation);
    if(
    hFile != INVALID_HANDLE_VALUE)
    {
    while(
    FindNextFile(hFile, &FileInformation))
    {
    if(
    strstr(FileInformation.cFileName,_filename))
    {
    sprintf(m_chFileName,"%s%s%s",strRootDirectory.c_str (),"\\",FileInformation.cFileName);
    :
    eleteFile(m_chFileName);
    }

    if(!
    strstr(FileInformation.cFileName ,"."))
    {
    strPattern=strRootDirectory+"\\"+FileInformation.cFileName;
    DeleteFile(strPattern.c_str (),_filename);
    }

    }
    }

    return 
    0;

    Mick, Mick !!!!!!!! No matter how you are....
    I win this time, admit it or not...

    Regards,

    Fiona

  7. #7
    Join Date
    Dec 2003
    Posts
    220
    Since I am in an excitement, I have not checked that code snip, but I think you can do it yourself right ?

    Hope that helps

    // By the way, that code snip is actually from a member on this board who used to give me when iwas searching for a a file in directory... That guy really had a nice intention

    Regards,
    homestead

  8. #8
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537

    Re: Here the code

    Originally posted by Homestead
    [
    Mick, Mick !!!!!!!! No matter how you are....
    I win this time, admit it or not...

    Regards,

    Fiona
    he already knows the filenames, so findfirstfile would be extraneous.

  9. #9
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    I think it is probably caused by not closing the file handle, like fclose(), before using remove().

    The return value of remove() reveals the status of the operation. If removing the file is successful, the return value is 0. Otherwise it is -1 and errno is set either to EACCES to indicate that the path specifies a read-only file, or to ENOENT to indicate that the filename or path was not found or that the path specifies a directory.
    Last edited by Kheun; January 19th, 2004 at 11:41 PM.

  10. #10
    Join Date
    Nov 2003
    Posts
    19
    Thanks everyone.
    I think my application is holding the file, that's why I can't delete it.

    My program looks something like this:-

    char* file[] = {"test1.txt"};
    void process()
    {
    TestRecord* parser;
    RecordtoTestRecord* record;
    char* ptr;
    ptr = file[1];

    if(parser!=NULL)
    {
    delete parser;
    parser = NULL;
    }

    while(1)
    {
    parser = new TestRecord;
    record = parser->parsefile(ptr);
    record->processBlockinfile();
    :
    :
    free(ptr); //probably I don't need to free this pointer
    remove(file[1]);
    }
    }

    I am not sure who is holding the file[1].
    I thought the *parser has already pass all the data to record.

  11. #11
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    where do you open the file? close the handle returned from whatever open API you are using.

  12. #12
    Join Date
    Nov 2003
    Posts
    19
    is the ptr a handler?
    i try to free it but i got debug assertion error.

  13. #13
    Join Date
    Nov 2003
    Posts
    19
    I fopen and fclose the file before i called that process().
    before the process() begin, i still manage to remove(file);
    but right after the parser->parsefile(ptr); i couldn't remove the file anymore.

  14. #14
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    probably because your filename is invalid.

    what exactly are you trying to do? Your file operations are pointing to the 1 index in test.txt, which is 'e' are you trying to create a file called 'e' or test.txt?

    char file [] = {"test.txt"};
    ...
    ...
    ...
    remove(file);

  15. #15
    Join Date
    Dec 2003
    Posts
    220
    If possible, can you post your remove function, I just wonder how you made it, there might be some error in it...

    Regards,

    Fiona

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured