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\Cl ient.c(150): error: expression must be a modifiable lvalue
recvbuf2 = HeapAlloc(GetPr ocessHeap(), HEAP_ZERO_MEMOR Y,sizeof(char_3 6));
char_36 i have used
typedef struct data
{
char a[36];
}char_36;
what could the error.
7 4656 Banfa 9,065
Recognized Expert Moderator Expert
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 __tmainCRTStart up _CrtIsValidHeap Pointer Normal 0
how to debug the program
Banfa 9,065
Recognized Expert Moderator Expert
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
waitformultiple instance()
there are two threads with the handels access and hThread now how should i give the wait,
waitformultiple instance(2,hThr ead,access,TRUE ,INFINITE) is this ok or how to specify two handels.
Banfa 9,065
Recognized Expert Moderator Expert
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 waitformultiple objects(2,hthre ad,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
Recognized Expert Moderator Expert
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 this
bad style?
For example:
class myClass {
public:
int myInt;
|
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 that I found inside of a
string.
Any ideas?
|
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 matrices, but as matrix dimension
grows up it crashes in deterministic locations depending on data,
machine and OS.
In particular, this happens when I test it with 3000x3000 matrices
(which take up more than 30MB) under three different machines/OSes....
|
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 ends. This
program originally written in 2003 and I rewrote it in 2005 earlier this
year. I don't know how long this has been going on but we just noticed
today that the program was using over a gig of virtual memory. Real
memory was quite...
| |
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 (I am new to Windows so not
sure). We are currently bouncing the instance to overcome this error. This
generally happen at the end of business day only (So maybe memory
might be getting used up?). We have already increased the statement heap &
...
|
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 have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:
void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;
|
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. As per my observation during these operations,memory keep on allocating on heap and never release untill Garbage collector gets called by system. I also set all the objects to nothing after its use, but still it is not releasing the memory. Can...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |