views:

144

answers:

2

Working on a WinPCap project. Trying to do some basic pointer and memory operations and having lots of errors.

I've included the two lines I'm trying to run along with the includes. The same lines in another VSC++ project work just fine. This is the error I am getting

Unhandled exception at 0x75a79617 in pktdump_ex.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012f8e4..

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include "DataTypes.h"
#include <sstream>

#include "EthernetLayer.h"

#include <pcap.h>

int* testPointer = new int[2];
delete[] testPointer;

EDIT: Found out something useful. The following code snippet is what is crashing the winpcap library.

EthernetStructPointers* testData;
testData = (EthernetStructPointers*)pkt_data;

EthernetStruct newData;
memcpy(newData.DEST_ADDRESS, testData->DEST_ADDRESS, 6);

These are the definitions of the structs.

struct EthernetStructPointers
{
    u_char DEST_ADDRESS[6];
    u_char SOURCE_ADDRESS[6];
    u_char TYPE[2];
};


struct EthernetStruct
{
    u_char DEST_ADDRESS[6];
    u_char SOURCE_ADDRESS[6];
    u_char TYPE[2];

    u_char* dataPointer;

    string DestAddress;
    string SourceAddress;
    string Type;

    int length;
};
A: 

std::bad_alloc should be thrown when you try to new something and have run out of memory. Can you check how much free memory is available to your process?

Paul Stephenson
Just tried running the above a few more times. It received packet data twice then crashed with a bad alloc. How do I check how much memory is available? I am running Windows 7 with 4 gigs of RAM.
bobber205
Can you open Task Manager (is there still one in Windows 7?), add as many memory-based columns as you can and re-run the test? If any memory values spike to huge amounts it may be a clue.
Paul Stephenson
Post an entire minimal code sample that produces the problem. Likely you have corrupted the heap or stack some place unrelated to the code you have shown.
nos
+2  A: 

My guess is the freestore is corrupted by one the previous statements (perhaps by an incorrect use of the pcap interface), and you only learn of the error on the next memory allocation or release, when the manager detects it and throws a bad alloc.

Todd Gardner
Have some answers.Posted them in the edit of the main post.
bobber205
Solved the problem. :DIt was me trying to do a memory copy myself later on in the loop instead of using memcpy. I was pretty sure I was doing it correctly but I guess not. Thanks for the help everyone!
bobber205