Compiler or code error?

This is a discussion on Compiler or code error? within the Windows Programming forums, part of the Platform Specific Boards category; Here I have a function that sends a request to a server to delete a file. But, it always crashes ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Question Compiler or code error?

    Here I have a function that sends a request to a server to delete a file. But, it always crashes on a certain line when compiled in debug mode and runs fine when compiled in release mode. Is this a compiler error? Here's my code:
    Code:
    void deleteFile(HWND hWnd)
    {
         //Get a handle to the listbox control
         HWND lb = GetDlgItem(hWnd, IDC_REMOTEFILES); //lb = listbox
    
         //Get the index of the listbox item
         int index = SendMessage(lb, LB_GETCURSEL, 0, 0);
    
         //Get the length of the item and allocate a string for it
         int len = SendMessage(lb, LB_GETTEXTLEN, index, 0);
         char* fNameBuf = new char[len];
         unsigned char nameLen = SendMessage(lb, LB_GETTEXT, index, (LPARAM)fNameBuf);
    
         unsigned long msgLen = 1;
         msgLen += sizeof(nameLen);
         msgLen += nameLen;
         server->send(htonl(msgLen));
    
         server->send(ICV_OP_DELETE);
         server->send(nameLen);
         server->send(fNameBuf);
    
         unsigned char result;
         server->receive((char*)&result, 1);
    
         if(result == ICV_SR_ERR)
              MessageBox(hWnd, "File could not be deleted.", "Error", MB_OK);
         else if(result == ICV_SR_OK)
              MessageBox(hWnd, "File successfully deleted.", "Success", MB_OK);
         else
              MessageBox(hWnd, "Unknown protocol error.", "Error", MB_OK);
    
         MessageBox(hWnd, "Going to delete fNameBuf", "c", MB_OK);
         delete[] fNameBuf;
         MessageBox(hWnd, "fNameBuf deleted", "k", MB_OK);
    }
    It always crashes between the 2 last messageboxes, right after I click ok on the "Going to delete fNameBuf". The crash thing that pops up says:
    Debug error!

    Program: (...)

    Damage: after Normal block (#22) at 0x00F10D90

    (abort, retry, ignore)
    Should I look to my code or my compiler for the problem? I really can't see anything wrong with the code, but I thought I'd ask just in case, since it's just about always my fault anyways

    **EDIT**
    Wasn't sure if this belonged on the c++, networking, or Windows board, but since the code involves Windows API I thought I'd put it under Windows... hope the mods don't mind.
    Last edited by Hunter2; 08-03-2003 at 04:26 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
     int len = SendMessage(lb, LB_GETTEXTLEN, index, 0);
    char* fNameBuf = new char[len];
    MSDN LB_GETTEXTLEN
    The return value is the length of the string, in TCHARs, excluding the terminating null character.
    You need +1.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks for all the help guys Guess I better be tracking that bounds overflow thinger down now

    **EDIT**
    Ahha... ahem... Oops, I forgot to add a "+1" for the null 0

    **EDIT2**
    Eep I didn't read wledge's post
    Last edited by Hunter2; 08-03-2003 at 07:02 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 03:39 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 11:55 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21