memset() run time error
-
Hi all, I am getting some "Access violation writing " for memset for every 30 min after i run my application. And the cursor goes to the below line:
memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));
Here is the function i am using i am trying,and this function i am calling in timer:
#define BUF_SIZE 2048
const int max_msg = 2048;
char *pSharedBuffer2 = (char*)malloc(max_msg);
int WriteToShared()
{HANDLE handleMappedFile; LPVOID lpMsgBuf; handleMappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL, PAGE\_READWRITE, HIWORD(BUF\_SIZE), LOWORD(BUF\_SIZE), szName4); if (handleMappedFile == NULL) { printf("Could not create file mapping object.\\n"); LocalFree( lpMsgBuf ); } memset(pSharedBuffer2, 0, sizeof(\*pSharedBuffer2)); pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE); if (pSharedBuffer2 == NULL) { printf("Could not map view of file.\\n"); CloseHandle(handleMappedFile); } sprintf(i,"%d",Test); addMsg1(i, pSharedBuffer2); return 0;
OnTimer()
{
WriteToShared()
}}
Can anyone guide,whats exactly going wrong. Thanks Sharan
may be give memset(pSharedBuffer2, 0, max_msg );
-
Hi all, I am getting some "Access violation writing " for memset for every 30 min after i run my application. And the cursor goes to the below line:
memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));
Here is the function i am using i am trying,and this function i am calling in timer:
#define BUF_SIZE 2048
const int max_msg = 2048;
char *pSharedBuffer2 = (char*)malloc(max_msg);
int WriteToShared()
{HANDLE handleMappedFile; LPVOID lpMsgBuf; handleMappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL, PAGE\_READWRITE, HIWORD(BUF\_SIZE), LOWORD(BUF\_SIZE), szName4); if (handleMappedFile == NULL) { printf("Could not create file mapping object.\\n"); LocalFree( lpMsgBuf ); } memset(pSharedBuffer2, 0, sizeof(\*pSharedBuffer2)); pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE); if (pSharedBuffer2 == NULL) { printf("Could not map view of file.\\n"); CloseHandle(handleMappedFile); } sprintf(i,"%d",Test); addMsg1(i, pSharedBuffer2); return 0;
OnTimer()
{
WriteToShared()
}}
Can anyone guide,whats exactly going wrong. Thanks Sharan
manju 3 wrote:
memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));
That is probably NOT what you intended to do, because it sets just
1
byte (sizeof(char)
is ONE). Did you meanmemset(pSharedBuffer2, 0, max_msg);
?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
manju 3 wrote:
memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));
That is probably NOT what you intended to do, because it sets just
1
byte (sizeof(char)
is ONE). Did you meanmemset(pSharedBuffer2, 0, max_msg);
?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, Thanks for your reply.Even i tried with
memset(pSharedBuffer2, 0, max_msg);
but getting the same "access violation for writing" Thanks Sharan
What is
i
(i.e. how is it declared/defined?)?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, Thanks for your reply.Even i tried with
memset(pSharedBuffer2, 0, max_msg);
but getting the same "access violation for writing" Thanks Sharan
and what is addMsg1() and what does it do? It is blowing up in there? Also, you have a macro that defines to 2048 and max_msg that is set to 2048 and then use both in the code, interchangably. Pick one and use it exclusively otherwise you'll get bit one of these days.
-
Hi all, I am getting some "Access violation writing " for memset for every 30 min after i run my application. And the cursor goes to the below line:
memset(pSharedBuffer2, 0, sizeof(*pSharedBuffer2));
Here is the function i am using i am trying,and this function i am calling in timer:
#define BUF_SIZE 2048
const int max_msg = 2048;
char *pSharedBuffer2 = (char*)malloc(max_msg);
int WriteToShared()
{HANDLE handleMappedFile; LPVOID lpMsgBuf; handleMappedFile = CreateFileMapping(INVALID\_HANDLE\_VALUE, NULL, PAGE\_READWRITE, HIWORD(BUF\_SIZE), LOWORD(BUF\_SIZE), szName4); if (handleMappedFile == NULL) { printf("Could not create file mapping object.\\n"); LocalFree( lpMsgBuf ); } memset(pSharedBuffer2, 0, sizeof(\*pSharedBuffer2)); pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE); if (pSharedBuffer2 == NULL) { printf("Could not map view of file.\\n"); CloseHandle(handleMappedFile); } sprintf(i,"%d",Test); addMsg1(i, pSharedBuffer2); return 0;
OnTimer()
{
WriteToShared()
}}
Can anyone guide,whats exactly going wrong. Thanks Sharan
manju 3 wrote:
pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE);
This doesn't really make a lot of sense since you are just copying the pointer, not the contents! First, you overwrite the only pointer to a memory block on the heap that you allocated with malloc, so you create a memory leak, right there. Second, while the pointer is global and will survive the call to WriteToShared, the HANDLE you get from
MapViewOfFile()
will not: at the latest, it will be invalidated when you leave the function, upon which yourhandleMappedFile
gets destroyed. At that point,pSharedBuffer2
will point to garbage! The address stored therin is invalid, and you may not access this memory. And this, exactly, happens, when next you callmemset
! That is your crash. -
What is
i
(i.e. how is it declared/defined?)?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
manju 3 wrote:
pSharedBuffer2 = (char\*)MapViewOfFile(handleMappedFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, BUF\_SIZE);
This doesn't really make a lot of sense since you are just copying the pointer, not the contents! First, you overwrite the only pointer to a memory block on the heap that you allocated with malloc, so you create a memory leak, right there. Second, while the pointer is global and will survive the call to WriteToShared, the HANDLE you get from
MapViewOfFile()
will not: at the latest, it will be invalidated when you leave the function, upon which yourhandleMappedFile
gets destroyed. At that point,pSharedBuffer2
will point to garbage! The address stored therin is invalid, and you may not access this memory. And this, exactly, happens, when next you callmemset
! That is your crash. -
CPallini wrote:
What is
i
"i" is a flag may be 0 or 1. i am making it 0 initially,and later making it 1 and writing to shared memory
Then
sprintf(i,"%d",Test);
is evil (
sprintf
assumesi
is an array of characters).If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Easy: don't call
memset
! Two possibilities: 1. if you don't usepShardedBuffer2
outside this function, then (a) declare it inside the function as a local parameter and (b) don't malloc, and don't memset (in this case I really don't know what you do that for) 2. if you do use it elsewhere, then you must copy the data, not the pointer! You should be aware though, that changes to that copy won't affect the original data, you can use that copy only to read from! Anyway, if you copy data to your buffer, memset is not required - the value you set your buffer to gets overwritten anyway! Note that I have no idea how to go about copying the data, asMapViewOfFile
returns aHANDLE
, not a string, and does not provide any information about the structure or size of that data. Accessing it like a string sounds like calling for trouble, especially if there are 0 bytes within. I'm not familiar with this function or the data structure it delivers, but I suggest you read up on it and how to use it properly. What you need to do may also depend on how you use the data later, such as in your call toaddMsg1()
. What does it do? -
Hi, Thanks for your reply.Even i tried with
memset(pSharedBuffer2, 0, max_msg);
but getting the same "access violation for writing" Thanks Sharan