Forum Topic - when I change the contents of a " PtImageArea ", the application failed and eixt .Is there any thing wrong with the following code ?: (3 Items)
   
when I change the contents of a " PtImageArea ", the application failed and eixt .Is there any thing wrong with the following code ?  
////////////////////////////////////////////////////////////////////////////////////////////////////

PhImage_t *load_image( char* filename )
{
	PhImage_t	*p_phimage;
	FILE 		*image_file;
	int			image_len;
	char		*image_data = NULL;

	image_file = fopen( filename , "r");
	if( image_file ){
		fseek( image_file , 0 , SEEK_SET );
		fseek( image_file , 0 , SEEK_END );

		image_len  = ftell( image_file );
		image_data = malloc( image_len );

		fseek( image_file , 54 , SEEK_SET );
		fread( image_data , image_len - 54 , 1 , image_file);

		p_phimage = PhCreateImage( NULL , 640 , 480 , Pg_IMAGE_DIRECT_888 , 0 , 0 , 0 );
		memcpy( p_phimage->image , image_data , image_len );
		printf(" load image successfully!\n");
		return p_phimage;
	}
	else
	{
		return NULL;
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int  chg_dis_time( RtTimer_t * p_timer, void * p_data)
{
	char 			 temp[20];
	static int	 	 i;
	static PhImage_t *p_image1;
	static PhImage_t *p_image2;

	if( 0 == i )
	{
		p_image1 = load_image( "/tmp/door_mark1.bmp" );
		p_image2 = load_image( "/tmp/door_mark2.bmp" );
	}
	i += 1;
	sprintf( temp , "%d" , i );
	PtSetResource( ABW_text_time , Pt_ARG_TEXT_STRING , temp , 5 );
	if( 1 == i%2)
	{
		if( p_image1 )
		{
			PtSetResource( ABW_image_canvas , Pt_ARG_IMAGEAREA_IMAGE , NULL , 0 );
		}
	}
	else
	{
		if( p_image2 )
		{
			PtSetResource( ABW_image_canvas , Pt_ARG_IMAGEAREA_IMAGE , p_image2 , 0 );
		}
	}
	return 0;
}
Re: when I change the contents of a " PtImageArea ", the application failed and eixt .Is there any thing wrong with the following code ?  
Hi Robort,

PhCreateImage() call sets clearing flags in p_phimage structure. This clearing proceed after PtSetResource. You can set 
flags to off state just after PhCreateImage(), e.g.:

     p_phimage->flags &= !Ph_RELEASE_IMAGE_ALL;

As a result the memory won’t be cleared. Don’t forget to clear memory when loaded image will not be needed.

Respectfully,
Oleg

14 июля 2014 г., в 11:13, robort smith <community-noreply@qnx.com> написал:

> ////////////////////////////////////////////////////////////////////////////////////////////////////
> 
> PhImage_t *load_image( char* filename )
> {
> 	PhImage_t	*p_phimage;
> 	FILE 		*image_file;
> 	int			image_len;
> 	char		*image_data = NULL;
> 
> 	image_file = fopen( filename , "r");
> 	if( image_file ){
> 		fseek( image_file , 0 , SEEK_SET );
> 		fseek( image_file , 0 , SEEK_END );
> 
> 		image_len  = ftell( image_file );
> 		image_data = malloc( image_len );
> 
> 		fseek( image_file , 54 , SEEK_SET );
> 		fread( image_data , image_len - 54 , 1 , image_file);
> 
> 		p_phimage = PhCreateImage( NULL , 640 , 480 , Pg_IMAGE_DIRECT_888 , 0 , 0 , 0 );
> 		memcpy( p_phimage->image , image_data , image_len );
> 		printf(" load image successfully!\n");
> 		return p_phimage;
> 	}
> 	else
> 	{
> 		return NULL;
> 	}
> }
> ////////////////////////////////////////////////////////////////////////////////////////////////////
> int  chg_dis_time( RtTimer_t * p_timer, void * p_data)
> {
> 	char 			 temp[20];
> 	static int	 	 i;
> 	static PhImage_t *p_image1;
> 	static PhImage_t *p_image2;
> 
> 	if( 0 == i )
> 	{
> 		p_image1 = load_image( "/tmp/door_mark1.bmp" );
> 		p_image2 = load_image( "/tmp/door_mark2.bmp" );
> 	}
> 	i += 1;
> 	sprintf( temp , "%d" , i );
> 	PtSetResource( ABW_text_time , Pt_ARG_TEXT_STRING , temp , 5 );
> 	if( 1 == i%2)
> 	{
> 		if( p_image1 )
> 		{
> 			PtSetResource( ABW_image_canvas , Pt_ARG_IMAGEAREA_IMAGE , NULL , 0 );
> 		}
> 	}
> 	else
> 	{
> 		if( p_image2 )
> 		{
> 			PtSetResource( ABW_image_canvas , Pt_ARG_IMAGEAREA_IMAGE , p_image2 , 0 );
> 		}
> 	}
> 	return 0;
> }
> 
> 
> 
> _______________________________________________
> 
> Photon microGUI
> http://community.qnx.com/sf/go/post111044
> To cancel your subscription to this discussion, please e-mail photon-graphics-unsubscribe@community.qnx.com

Re: when I change the contents of a " PtImageArea ", the application failed and eixt .Is there any thing wrong with the following code ?  
Thank you very much!!!
I guess you have get the key point of this problem!