new/delete causes access violation

Ok, this is perplexing me... The code below is in a DLL, and when my console application calls this code it is suddenly throwing an access violation at the line delete[] lpBuffer. I have been using this code all day and have not changed it at all. Until now, it had been working just fine.

Access Violation Message

Unhandled exception at 0x6948b1a5 in rhcopy.exe: 0xC0000005: Access violation reading location 0x4de1c37f.

Library Code

#define MAX_PACKET_SIZE  0x3FFF

DWORD MyClass::GetFile( LPCSTR lpszRemoteFile, LPCSTR lpszLocalFile )
{
    LPBYTE lpBuffer = NULL;

    // ...

    lpBuffer = new BYTE[MAX_PACKET_SIZE];

    // ...

    if( NULL != lpBuffer )
        delete[] lpBuffer;

    // ...
}

Am I doing something wrong?

On a side note: I have been thinking about converting lpBuffer into a vector. Opinions?

edit

I want to thank you guys for your help! But apparently... That's not where the problem is. problem is actually a printf() statement in the calling application that occurs right after the call to GetFile(...). I apologize for the confusion. It would seem that Microsoft's Debugging tool is not pointing to the line that caused the error, but rather the last line that executed. I have voted to close the question.

Answers


There is nothing wrong in the code you've shown here.

Two possibilities come to mind:

  1. The error is not actually in the delete but in the code just on either side.

  2. You've done something to corrupt the heap, by using an invalid pointer that overwrites some random memory or deleting a pointer more than once.

Neither of these problems would be caught by switching to RAII (i.e. vector) in the displayed code, but might improve things if you used them consistently in the rest of your code.


Need Your Help

Available space left on drive - WinAPI - Windows CE

winapi pinvoke windows-ce diskspace

I've forgotten the WinAPI call to find out how much space is remaining on a particular drive and pinvoke.net isn't giving me any love.

Objective-C - Category to return a specific font?

objective-c categories uifont

I am trying to write a category and override fontWithName:size in UIFont. I am doing this for unit testing to avoid crashes caused by font names that are available on ipad and not on OSX. So I need...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.