[Mono-dev] Marshalling (Segmentation fault)

Abir Bhattacharya abirb at 2pirad.com
Thu Oct 11 08:42:30 EDT 2007


Hi,

Actually I have another 'C' File which allocates this buffer : 

This captures allocates on a per frame basis.

void fcapture()
{
	int fd;

	struct video_mbuf mbuf;
	char *buf0, *buf1/*, *buf2, *buf3*/;
	int size;
	//unsigned char *tmp;
	
	struct video_mmap mm;

	int frame;

	fd = open("/dev/video0", O_RDWR);
	if(fd <= 0)
	{
		printf("open");
		exit(1);
	}
	
	if(ioctl(fd, VIDIOCGMBUF, &mbuf) < 0)
	{
		perror("ioctl VIDIOCGMBUF");
		exit(1);
	}
	printf("ioctl VIDIOCGMBUF: done\n");
	
	//use mmap to get a pointer to each buffer.
	buf0 = (unsigned char*) mmap(0, mbuf.size, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
	buf0 = buf0 + mbuf.offsets[0];
	buf1 = buf0 + mbuf.offsets[1];
	size = (320 * 240 * 3) >> 1;
	//tmp = (unsigned char*) malloc(size);
	/*
	buf2 = buf0 + mbuf.offsets[2];;
	buf3 = buf0 + mbuf.offsets[4];;
	*/

	mm.height = 240;
	mm.width = 320;
	mm.format = VIDEO_PALETTE_YUV420P;
	mm.frame = 0;
	if(ioctl(fd, VIDIOCMCAPTURE, &mm) < 0)
	{
		perror("ioctl VIDIOCMCAPTURE 0");
		exit(1);
	}
	printf("ioctl VIDIOCMCAPTURE 0: done\n");
	
	while (1)
	{
		mm.height = 240;
		mm.width = 320;
		mm.format = VIDEO_PALETTE_YUV420P;
		mm.frame = 1;
		if(ioctl(fd, VIDIOCMCAPTURE, &mm) < 0)
		{
			perror("ioctl VIDIOCMCAPTURE 1");
			exit(1);
		}
		printf("ioctl VIDIOCMCAPTURE 1: done\n");

		frame = 0;
		if(ioctl(fd, VIDIOCSYNC, &frame) < 0)
		{
			perror("ioctl VIDIOCSYNC 0");
			exit(1);
		}
		printf("ioctl VIDIOCSYNC 0: done\n");
		//memset(tmp, 0x00, size);
		//memcpy(tmp,	buf0, size);
		//printf ("%u %u %u %u\n", tmp[0], tmp[1], tmp[2], tmp[3]);
		//writepicture(tmp, size);
		SDL_mutexP(mutex);
		buffer[bufferlength] = (unsigned char*) malloc(size);
		memset(buffer[bufferlength], 0x00, size);
		memcpy(buffer[bufferlength++],	buf0, size);
		SDL_mutexV(mutex);

		mm.height = 240;
		mm.width = 320;
		mm.format = VIDEO_PALETTE_YUV420P;
		mm.frame = 0;
		if(ioctl(fd, VIDIOCMCAPTURE, &mm) < 0)
		{
			perror("ioctl VIDIOCMCAPTURE 0");
			exit(1);
		}
		printf("ioctl VIDIOCMCAPTURE 0: done\n");

		frame = 1;
		if(ioctl(fd, VIDIOCSYNC, &frame) < 0)
		{
			perror("ioctl VIDIOCSYNC 1");
			exit(1);
		}
		printf("ioctl VIDIOCSYNC 1: done\n");
		//memset(tmp, 0x00, size);
		//memcpy(tmp,	buf1, size);
		//printf ("%u %u %u %u\n", tmp[0], tmp[1], tmp[2], tmp[3]);
		//writepicture(tmp, size);
		SDL_mutexP(mutex);
		buffer[bufferlength] = (unsigned char*) malloc(size);
		memset(buffer[bufferlength], 0x00, size);
		memcpy(buffer[bufferlength++],	buf0, size);
		SDL_mutexV(mutex);
	}
	
	close(fd);
}

-----Original Message-----
From: mono-devel-list-bounces at lists.ximian.com
[mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Robert Jordan
Sent: Thursday, October 11, 2007 5:52 PM
To: mono-devel-list at lists.ximian.com
Subject: Re: [Mono-dev] Marshalling (Segmentation fault)

Hi,

> struct fgrab {
> 	int bufferlength;
> 	unsigned char *buffer[1000];
> } typedef fgrab_struct;
> 
 >
> int bufferlength = 0;
> unsigned char *buffer[1000];
> 
> extern SDL_mutex *mutex;
> 
> 
> fgrab_struct gbuffergrab() {
> 	fgrab_struct fgbuffer;
> 
> 	int index;
> 
> 	SDL_mutexP(mutex);
> 
> 	fgbuffer.bufferlength = bufferlength;
> 	//fgbuffer.buffer = buffer;
> 	for (index = 0; index < bufferlength; index++) {
> 		fgbuffer.buffer[index] = buffer[index];
> 	}
> 
> 	bufferlength = 0;
> 
> 	SDL_mutexV(mutex);
> 
> 	return fgbuffer;
> }


I don't see where you're allocating the buffer, so
I think that function simply doesn't work.

Are you sure you want "unsigned char *buffer[1000]"?
I guess it should be "unsigned char buffer[1000]".

Fix this first and the p/invoke will work as well.

Robert


Abir Bhattacharya wrote:
> The 'C' prototype :
> 
> 
> 
> #ifndef PICTURE_H
> #define PICTURE_H
> 
> #include "def.h"
> 
> 
> struct fgrab {
> 	int bufferlength;
> 	unsigned char *buffer[1000];
> } typedef fgrab_struct;
> 
> 
> fgrab_struct fbuffergrab();
> void fbufferflush(fgrab_struct fgbuffer);
> 
> #endif
> 
> -----Original Message-----
> From: mono-devel-list-bounces at lists.ximian.com
> [mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Robert
Jordan
> Sent: Thursday, October 11, 2007 4:32 PM
> To: mono-devel-list at lists.ximian.com
> Subject: Re: [Mono-dev] Marshalling (Segmentation fault)
> 
> Abir Bhattacharya wrote:
>> [DllImport ("lib.so")]
>>
>> Private static extern ImgStruct gbuffergrab(); // gbuffergrab is the 'C'
>> method which returns type struct
> 
> Please post the C prototype as well.
> 
> Robert
> 
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
> 
> 
> 
> 
> -- Visit us at http://www.2pirad.com/ --

_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list




-- Visit us at http://www.2pirad.com/ --



More information about the Mono-devel-list mailing list