15 May, 2012, Baiou wrote in the 1st comment:
Votes: 0
I did some searching on the web, but came up short. I need some help figuring this out. I'm just trying to pass char data packaged in a structure to another function elsewhere, but it's not working out. The data doesn't return null, but the compiler does pull these up:

"Access violation reading location 0xcccccccc." – Makes me think uninitialized variables somewhere.

Also:

"mov eax,dword ptr [ecx] ; read 4 bytes" – pointed to strlen.asm

Prior to passing

typedef struct _RECEIVEMSG {
char* lpRecvData;
unsigned long ulRecvDataSize;
} RECEIVEMSG;

//– Inside of the function

RECEIVEMSG msg_ptr;

ZeroMemory( &msg_ptr, sizeof( RECEIVEMSG ) );

msg_ptr.recvData = new char[MAX_BUFFER_IN_SIZE];

// Data is fine here.

this->msg_handler_( (void*) &msg_ptr ); //<– This guy smh


Post passing to the msg_handler_ which just takes void* lpvMsg as a parameter.
RECEIVEMSG* msg_ptr = (RECEIVEMSG*) lpvMsg;

std::cout << lpvMsg->lpRecvData << std::endl; // – FAILS and CAUSES ERROR
15 May, 2012, Baiou wrote in the 2nd comment:
Votes: 0
Nevermind. Solved it.
15 May, 2012, kiasyn wrote in the 3rd comment:
Votes: 0
What was it
15 May, 2012, Baiou wrote in the 4th comment:
Votes: 0
there was a typo in the loop prior to any of these which compiled okay, but was causing the data to be used before it was set up. Would basically loop once all the way through before being selective. Shrug.
15 May, 2012, Baiou wrote in the 5th comment:
Votes: 0
However there is another issue, but I decided to forego it for now and just use std::strings.

struct SENDMSG {
char* lpSendData;
unsigned long ulSendDataSize;
};


Initializes fine and what not, but passing with (std::string).data() or .c_str() will return mumble jumble instead of the actual characters presented. Any idea? memaddress maybe instead of actual values?
16 May, 2012, Tyche wrote in the 6th comment:
Votes: 0
That's just a structure declaration. Exactly how it's initialized and accessed isn't apparent from what you've posted.
16 May, 2012, Baiou wrote in the 7th comment:
Votes: 0
SENDMSG smsg;

ZeroMemory( &smsg, sizeof( SENDMSG ) );
lpSendData = new char[MAX_BUFFER_OUT_SIZE];

std::string buffer( "anything" );

lpSendData = (char*) buffer.c_str() // or buffer.data() – does the same.
ulSendDataSize = buffer.size();

function( lpSendData, ulSendDataSize ); // function works fine standalone


cout'ing lpSendData reveals IIIIIIII, of course they're not I's, but I look-a-likes. Let's just call them "me"s.
16 May, 2012, Tyche wrote in the 8th comment:
Votes: 0
There isn't any context or sequence in any code you have posted.
Good luck.
16 May, 2012, Caius wrote in the 9th comment:
Votes: 0
Hard to say exactly what's the problem from what you've posted. Some thoughts, though. First you allocate a block of memory to lpSendData, then you assign buffer.c_str() to it. This means that you have a memory leak, because the original memory you allocated can no longer be accessed. Perhaps you meant to copy buffer into lpSendData rather than just assigning it? strcpy(lpSendData, buffer.c_str()) is probably what you want. Be very careful with using c_str(). The data it points to is temporary. Any time you call a non-const function on the string class, the pointer may become invalid. Always assume it will. Also, if "function" changes the contents of lpSendData you're in trouble, because then you're also messing with the contents of buffer. So copying it is often best.

Btw, c_str() and data() are not equivalent. c_str() returns a null-terminated string, but data() does not.

Not sure if any of this helps.
16 May, 2012, Caius wrote in the 10th comment:
Votes: 0
Ok, my code also causes a mem-leak :p

So try this: sprintf(lpSendData, "%s", buffer.c_str())
17 May, 2012, Baiou wrote in the 11th comment:
Votes: 0
Awesome Caius, thanks. I'll give that a try and see what happens.
0.0/11