hi,
the error i am getting in main for allocating heap memory for threads is -
#include <stdio.h>
-
#include "winsock2.h"
-
#include <stdlib.h>
-
#include <windows.h>
-
#include <strsafe.h>
-
#define BUF_SIZE 255
-
#define nofthreads 2
-
-
typedef struct data
-
{
-
char a[36];
-
}char_36;
-
// Initialize Winsock.
-
WSADATA wsaData1,wsaData2;
-
// Create a socket.
-
SOCKET m_socket1,m_socket2;
-
SOCKADDR_IN client1Service,client2Service;
-
BOOL wiatall = TRUE;
-
CRITICAL_SECTION cs;
-
-
// Send and receive data.
-
//int bytesSent;
-
int bytesRecv1,bytesRecv2 = SOCKET_ERROR;
-
//char sendbuf[32] = "Client: Sending data.";
-
char recvbuf1[36] = "";
-
char recvbuf2[36] = "";
-
-
DWORD WINAPI workerProc( LPVOID lpParam )
-
{
-
char recvbuf1[36] = "";
-
int status ;
-
size_t cchStringSize;
-
int iResult;
-
-
TCHAR msgBuf[BUF_SIZE];
-
-
-
-
iResult = WSAStartup( 0X0202, &wsaData1 );
-
if ( iResult != NO_ERROR )
-
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("error at WSASTARTUP"));
-
-
-
-
m_socket1 = socket( AF_INET, SOCK_STREAM,IPPROTO_TCP);
-
-
if ( m_socket1 == INVALID_SOCKET )
-
{
-
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("error at socket"));
-
-
WSACleanup();
-
return(0);
-
}
-
-
-
client1Service.sin_family = AF_INET;
-
client1Service.sin_addr.s_addr = inet_addr( "192.168.0.239" );
-
client1Service.sin_port = htons(27015);
-
-
do
-
{
-
status = connect( m_socket1, (SOCKADDR *) &client1Service, sizeof(client1Service) );
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("failed to connect"));
-
WSACleanup();
-
return(0);
-
}while(status == SOCKET_ERROR);
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("connected to client 1"));
-
-
while( bytesRecv1 == SOCKET_ERROR )
-
{
-
-
bytesRecv1 = recv( m_socket1, recvbuf1, 36, 0 );
-
if ( bytesRecv1 == 0 || bytesRecv1 == WSAECONNRESET )
-
{
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("connection closed"));
-
break;
-
}
-
if (bytesRecv1 < 0)
-
return(0);
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("bytes received"));
-
}
-
HeapFree(GetProcessHeap(), 0, recvbuf1);
-
-
return(0);
-
}
-
-
DWORD WINAPI ThreadProc( LPVOID lpParam )
-
{
-
TCHAR msgBuf[BUF_SIZE];
-
size_t cchStringSize;
-
char recvbuf2[36] = "";
-
int iResult = WSAStartup( 0X0202, &wsaData2 );
-
if ( iResult != NO_ERROR )
-
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("error at WSASTARTUP"));
-
-
m_socket2 = socket( AF_INET, SOCK_STREAM,IPPROTO_TCP);
-
-
if ( m_socket2 == INVALID_SOCKET )
-
{
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("error at socket"));
-
WSACleanup();
-
return(0);
-
}
-
-
-
client2Service.sin_family = AF_INET;
-
client2Service.sin_addr.s_addr = inet_addr( "192.168.0.091" );
-
client2Service.sin_port = htons(27016);
-
-
do
-
{
-
connect( m_socket2, (SOCKADDR *) &client2Service, sizeof(client2Service) );
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("failed to connect"));
-
WSACleanup();
-
return(0);
-
}while(connect == SOCKET_ERROR);
-
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Connected to client 2"));
-
while( bytesRecv2 == SOCKET_ERROR )
-
{
-
-
bytesRecv2 = recv( m_socket2, recvbuf2, 36, 0 );
-
if ( bytesRecv2 == 0 || bytesRecv2 == WSAECONNRESET )
-
{
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("connection closed"));
-
break;
-
}
-
if (bytesRecv2 < 0)
-
return(0);
-
StringCchPrintf(msgBuf, BUF_SIZE, TEXT("bytes received"));
-
}
-
HeapFree(GetProcessHeap(), 0, recvbuf2);
-
-
return(0);
-
}
-
-
-
-
int main()
-
{
-
DWORD i=1;
-
HANDLE hThread,access;
-
DWORD dwThreadId,Idthread;
-
int Prio=2;
-
recvbuf1 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,sizeof(char_36));
-
-
recvbuf2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,sizeof(char_36));
-
-
-
-
-
hThread = CreateThread(
-
NULL, // default security attributes
-
0, // use default stack size
-
ThreadProc, // thread function
-
recvbuf1, // argument to thread function
-
0, // use default creation flags
-
NULL); // returns the thread identifier
-
access = CreateThread(
-
NULL, // default security attributes
-
0, // use default stack size
-
workerProc, // thread function
-
recvbuf2, // argument to thread function
-
0, // use default creation flags
-
NULL); // returns the thread identifier
-
-
-
-
if (hThread == NULL)
-
{
-
ExitProcess(1);
-
}
-
if(access == NULL)
-
{
-
ExitProcess(2);
-
}
-
WaitForMultipleObjects(1,hThread,TRUE,INFINITE);
-
//WaitForSingleObject(hThread,INFINITE);
-
CloseHandle(hThread);
-
CloseHandle(access);
-
-
return(0);
-
}
-
\..\integration\integration\Client.c(150): error: expression must be a modifiable lvalue
recvbuf2 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,sizeof(char_36));
char_36 i have used
typedef struct data
{
char a[36];
}char_36;
what could the error.
7 4632 Banfa 9,065
Expert Mod 8TB
The error is in the way you declare your data -
char recvbuf1[36] = "";
-
char recvbuf2[36] = "";
-
-
DWORD WINAPI workerProc( LPVOID lpParam )
-
{
-
char recvbuf1[36] = "";
-
...
-
}
-
You have declared recvbuf1 and recvbuf2 as arrays of char, HeapAlloc returns a pointer (void *) and it sould be allocated to a variable with a pointer type not an array identifier.
Then additionally in workerProc (and in ThreadProc) you declare receiveBuf1 again hiding your global definition of it. If you want the data on the heap I would have thought you could declare and allocated it inside workerProc removing the need to global data.
The error is in the way you declare your data -
char recvbuf1[36] = "";
-
char recvbuf2[36] = "";
-
-
DWORD WINAPI workerProc( LPVOID lpParam )
-
{
-
char recvbuf1[36] = "";
-
...
-
}
-
You have declared recvbuf1 and recvbuf2 as arrays of char, HeapAlloc returns a pointer (void *) and it sould be allocated to a variable with a pointer type not an array identifier.
Then additionally in workerProc (and in ThreadProc) you declare receiveBuf1 again hiding your global definition of it. If you want the data on the heap I would have thought you could declare and allocated it inside workerProc removing the need to global data.
i have removed the heap allocation entirely and made null in thread create and now there is a access violation error
Unhandled exception at 0x7c809576 in Client.exe: 0xC0000005: Access violation reading location 0x000017f4.
the thread s are being created and again in the main the error is
> 2968 __tmainCRTStartup _CrtIsValidHeapPointer Normal 0
how to debug the program
Banfa 9,065
Expert Mod 8TB
Have you also removed the calls to HeapFree?
Run the program in the debugger from your IDE. When it stops view the call stack to locate the point in your program where it has gone wrong.
i have removed the heapfree command also now the error lies in the
waitformultipleinstance()
there are two threads with the handels access and hThread now how should i give the wait,
waitformultipleinstance(2,hThread,access,TRUE,INFI NITE) is this ok or how to specify two handels.
Banfa 9,065
Expert Mod 8TB
The second parameter is wrong, it needs to be an array of handles to wait on. You are passing a single handle.
i changed the handels to array like hthread[0] and hthread[1] then given an array in the waitformultipleobjects(2,hthread,TRUE,INFINTE)
then while debuuging threads are created and close handel are also being executed while coming to the return state ment the error is like
Run-Time Check Failure #2 - Stack around the variable 'hThread.12113' was corrupted.
what could have possibly went wrong.
thanx for ur help bafna,
Banfa 9,065
Expert Mod 8TB
Most likely is that somehow you have written outside the bounds of the array.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Shailesh Humbad |
last post by:
If a class has no constructor/destructor, is not part of an
inheritance heirarchy, is not a template, and only contains members of
integral types, then is it okay to allocate it off the heap? Is...
|
by: Jonas Rundberg |
last post by:
Hi
I just started with c++ and I'm a little bit confused where stuff
go...
Assume we have a class:
class test {
private:
int arr;
};
|
by: smnoff |
last post by:
I have searched the internet for malloc and dynamic malloc; however, I still
don't know or readily see what is general way to allocate memory to char *
variable that I want to assign the substring...
|
by: Neclepsio |
last post by:
Hi everyone.
I've made a class Matrix, which contains a pointer to the data and some
methods which all return a copy of the matrix modified in some way. The
program works quite well for small...
|
by: cj |
last post by:
I have a 2005 TCP/IP server that creates a new thread to handle each
incoming TCP/IP request. Once the request has been answered by the
thread the TCP/IP socket is disconnected and the sub/thread...
|
by: kumarmdb2 |
last post by:
Hi guys,
For last few days we are getting out of private memory error. We have a
development environment. We tried to figure out the problem but we
believe that it might be related to the OS...
|
by: Chris Saunders |
last post by:
Here is the declaration of a struct from WinIoCtl.h:
//
// Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION
//
typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {
//
|
by: charlie |
last post by:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to...
|
by: amollokhande1 |
last post by:
Hi All,
We have ASP.Net based content management web application. We are performing lots of XML based operations like setting the innerxml, loading the xml, string replace operations on XML etc....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |