Reputation: 452
having some problems with my C code (kinda new to this language). I have the following code: ..
RTSPClient *clientInfo = (RTSPClient*) malloc(sizeof(RTSPClient));
if (!clientInfo)
{
printf("There wasn't enough memory to fufill the connection.\n");
continue;
}
clientInfo->socket = new_fd;
pthread_create(&thread, NULL, handleClientConnection, (void *) clientInfo);
...
where RTSPClient is the following
typedef struct {
int socket;
int session_id;
PlaybackTimer* playback_timer;
CvCapture* video;
} RTSPClient;
When we try to access the video field in the struct, we are getting seg faults. Were clearly not allocating things correctly, but wondering how we can fix this issue. Do I need to keep a global variable to the clientInfo object allocated before the thread was started, or do I need to allocate it statically?
Any help is appreciated.
Upvotes: 0
Views: 195
Reputation: 35408
RTSPClient *clientInfo = (RTSPClient*) malloc(sizeof(RTSPClient));
creates the memory only for the clientInfo
structure. You also need to allocate memory for CvCapture* video;
and all the other pointers. (if you do, please show more code how you work with the allocated structure).
And are you sure you meant continue
in case the memory allocation failed? I think you should abort your application if there is not enough memory... or nothing will work.
(And you don't need to cast the return value of malloc
in a C application)
Upvotes: 3