Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 959 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
Wierd invalid pointer. #340219
09/01/10 22:41
09/01/10 22:41
Joined: Feb 2005
Posts: 647
Williamsburg, VA USA
draculaFactory Offline OP
User
draculaFactory  Offline OP
User

Joined: Feb 2005
Posts: 647
Williamsburg, VA USA
The code below causes a crash when I click on my inventory panel and move it, the crash occurs during the "sort_panels" function, the panel pointer that is passed into the function becomes invalid somehow... Oddly enough, to test this, I put "reset(panel,SHOW);" just before "if(temp_panel.layer > panel.layer && temp_panel != panel)" to test and the function did hide the correct panel, meaning that the pointer is not actually invalid.

Code:
// inventory art
BMAP* backpack_bmp = "backpack_panel.tga";
BMAP* items_bmp = "item_icons.tga";
BMAP* item_x48_bmp = "item_button_x48.tga";


// container variable arrays
var slot_owner[500];
var slot_number[500];
var item_slot_id[500];
var icon_bmp_x[500];
var icon_bmp_y[500];
var item_mdl_id[500];
var item_stack[500];
var drop_sound[500];


// pointers used for the 6 inventory pack slots
PANEL* pack_slot1;
PANEL* pack_slot2;
PANEL* pack_slot3;
PANEL* pack_slot4;
PANEL* pack_slot5;
PANEL* pack_slot6;


// helps determine the clicked slot number
var panel_slot_start = 0;

STRING* welcome_str = "Welcome";

TEXT* welcome_text =
{
  layer = 1;
  pos_x = 10;
  pos_y = 10;
  string (welcome_str,"this is","a","TEXT");
  flags = CENTER_X | TRANSLUCENT | SHOW;

}


function sort_panels(PANEL* panel)
{
	var list_no = 0;
	var sss = 0;
	
	PANEL* temp_panel;
	
	while(list_no < total_panels)
	{
		temp_panel = ptr_for_handle(panel_list[list_no]);
		if(temp_panel.layer > panel.layer && temp_panel != panel)
		{
			layer_sort(temp_panel,temp_panel.layer - 1);
		}
		list_no += 1;
	}
	
	layer_sort(panel,total_panels);
}


//moves a panel with mouse
function move_panel(PANEL* panel)
{
	
	//local variables
	VECTOR panel_vec;
	VECTOR pointer_vec;
	VECTOR panel_offset;
	
	vec_set(panel_vec.x,vector(panel.pos_x,panel.pos_y,0));
	
	//get initial mouse position
	pointer_vec.x = mouse_pos.x;
	pointer_vec.y = mouse_pos.y;
	
	sort_panels(panel);
	
	// while mouse held, loop
	while(mouse_left == 1)
	{
		panel_offset.x = mouse_pos.x - pointer_vec.x;
		panel_offset.y = mouse_pos.y - pointer_vec.y;
		
		panel.pos_x = panel_vec.x + panel_offset.x;
		panel.pos_y = panel_vec.y + panel_offset.y;
		
		// restrict panel positions to on-screen
		panel.pos_x = clamp(panel.pos_x,0,screen_size.x - panel.size_x);
		panel.pos_y = clamp(panel.pos_y,0,screen_size.y - panel.size_y);
		
		wait(1);
	}
}


// creates an icon bmp cutout from the slot number
function get_icon_cutout(var slot_no)
{
	
	BMAP* icon_bmp;
	STRING* icon_str = "item_icons.tga";
	
	str_cat(icon_str,"#");
	str_cat(icon_str,str_for_num(NULL,icon_bmp_x[slot_no]));
	str_cat(icon_str,"#");
	str_cat(icon_str,str_for_num(NULL,icon_bmp_y[slot_no]));
	str_cat(icon_str,"#48#48");
	
	icon_bmp = bmap_create(icon_str);
	return(icon_bmp);
}


// inventory panel click event
function inv_panel_click(PANEL* inv_panel)
{
	
	var format;
	var pixel;
	
	// lock and read panel background to determine if it was actually clicked
	format = bmap_lock(inv_panel.bmap,0);
	pixel = pixel_for_bmap(inv_panel.bmap,mouse_pos.x - inv_panel.pos_x,mouse_pos.y - inv_panel.pos_y);
	bmap_unlock(inv_panel.bmap);
	
	// if panel was clicked
	if(pixel != 0)
	{
		
		// if cursor is empty
		if(item_slot_id[0] == 0)
		{
			move_panel(inv_panel);
		}
		
		// bring this panel to front
		sort_panels(inv_panel);
	}
}


// fills the inv panel with an amount of inventory slots
function create_slots(PANEL* inv_panel, var rows, var cols, var start_x, var start_y, var start_slot)
{
	
	var rows_created = 0;
	var cols_created = 0;

	var x_pos;
	var y_pos;
	var slot_step;

	x_pos = start_x;
	y_pos = start_y;
	slot_step = start_slot;

	while(rows_created < rows)
	{
		
		while(cols_created < cols)
		{
			pan_setwindow(inv_panel,0,x_pos,y_pos,48,48,items_bmp,icon_bmp_x[slot_step],icon_bmp_y[slot_step]);
			pan_setbutton(inv_panel,0,0,x_pos,y_pos,item_x48_bmp,item_x48_bmp,item_x48_bmp,item_x48_bmp,NULL,NULL,NULL);
			
			x_pos += 48;
			slot_step ++;
			
			cols_created ++;
			
			wait(1);
		}
		
		y_pos += 48;
		x_pos = start_x;
		
		cols_created = 0;
		rows_created ++;
		
		
		wait(1);
	}
}


// creates the pack panel
function create_inv_panel(var start_slot, var x_pos, var y_pos)
{

	PANEL* inv_panel = pan_create("bmap = backpack_bmp; event = inv_panel_click;",1);
	
	inv_panel.pos_x = x_pos;
	inv_panel.pos_y = y_pos;
	
	create_slots(inv_panel,4,8,64,168,start_slot);
	
	register_panel(inv_panel);
	
	layer_sort(inv_panel,2);
	
	return(inv_panel);
}


// starts the inventory system
function inv_startup()
{
	
	BMAP* drag_icon;
	
	pack_slot1 = create_inv_panel(16,0,32);
	set(pack_slot1,SHOW);
	
	pack_slot2 = create_inv_panel(49,128,48);
	set(pack_slot2,SHOW);
	
	while(1)
	{
		
		if(key_1 == 1)
		{
			snd_play(select_snd,vol_effects,0);
			toggle(pack_slot1,SHOW);
			while(key_1 == 1){wait(1);}
		}
		
		// determine starting slot number for inventory mouse functions
		if(mouse_panel == pack_slot1){panel_slot_start = 16;}
		if(mouse_panel == pack_slot2){panel_slot_start = 49;}
		if(mouse_panel == pack_slot3){panel_slot_start = 81;}
		if(mouse_panel == pack_slot4){panel_slot_start = 113;}
		if(mouse_panel == pack_slot5){panel_slot_start = 145;}
		if(mouse_panel == pack_slot6){panel_slot_start = 177;}
		
		// mouse icon
		if(slot_number[0] != 0)
		{
			
			// create the icon cutout referencing the cursor slot
			drag_icon = get_icon_cutout(0);
			
			// draw the icon under the mouse cursor
			draw_quad(drag_icon,vector(mouse_pos.x - 24,mouse_pos.y - 24,0),NULL,vector(48,48,0),NULL,NULL,100,0);
		}
		
		wait(1);
	}
}




Here's the clencher: I went through the entire code line by line and discovered that the crash was actually being caused by the "welcome_text" element at the top of the script; this text is not the original text that I was using, I pasted this text from the manual for testing to see if a text element was causing the problem. Sure enough, when I comment out "welcome_text", there is no crash when I click on a panel, and everything moves and sorts just fine. The text element is not being referenced by any code in this script.

What gives?

Last edited by draculaFactory; 09/01/10 22:41.

Making dreams come true... And being erroneously accused of software piracy by Conitec in the process.
Re: Wierd invalid pointer. [Re: draculaFactory] #340250
09/02/10 14:23
09/02/10 14:23
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
Maybe, in some other part of your code, welcome_str is set to NULL or an invalid pointer?

Re: Wierd invalid pointer. [Re: draculaFactory] #340252
09/02/10 14:28
09/02/10 14:28
Joined: Feb 2009
Posts: 2,154
Damocles_ Offline
Expert
Damocles_  Offline
Expert

Joined: Feb 2009
Posts: 2,154
strange.
The text could interact with its "layer" position
with the panels internally in the engine.
(beeing a part of a sorted list of layer elements)

Try fuzzing around with the layer parameter.

Re: Wierd invalid pointer. [Re: Damocles_] #340253
09/02/10 14:33
09/02/10 14:33
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271

Re: Wierd invalid pointer. [Re: Saturnus] #340257
09/02/10 15:09
09/02/10 15:09
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Ive looked at SOME of your code but not all, theres just so much.

But one thing that strikes comes to mind....

If your mouse pointer "crosses" the welcome text during the drag process,
then the 'mouse_panel' pointer will become null, because the welcome text
is in the way.
Could this be the cause?


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Wierd invalid pointer. [Re: EvilSOB] #340292
09/02/10 21:53
09/02/10 21:53
Joined: Feb 2005
Posts: 647
Williamsburg, VA USA
draculaFactory Offline OP
User
draculaFactory  Offline OP
User

Joined: Feb 2005
Posts: 647
Williamsburg, VA USA
Alright, I know that I should explain this part further. The system works fine when there is no text defined; if a text is defined, any text at all, anywhere in my code, the crash occurs. The welcome text is only there for the purpose of demonstrating the error. Even if I define a completely empty text, the crash still occurs.

There is a lot of code, I did narrow it down to a single line:
Code:
if(temp_panel.layer > panel.layer && temp_panel != panel)


The panel pointer is invalid ONLY in this line, it is not invalid before this line, and it is not invalid after this line. I am not referring to temp_panel, I am referring to the panel pointer that is passed into the function; ptr_for_handle works fine in this case.

Lukas, no welcome_str is not referenced at all by any code elsewhere.

Damocles, that occurred to me, but the text is not even in the same ball park, it is not registered or altered by anything.

EvilSOB, does mouse_panel work for text objects? The position of the text is in the upper left, the panel art is quite large and have dragged it around from multiple areas to make sure that I was actually hitting a panel. I have also tried to call the sort_panels function from the creation of the panel to ensure that no dragging has occured; again, with the text object defined I get a crash and without it, it works great.

Last edited by draculaFactory; 09/02/10 21:58.

Making dreams come true... And being erroneously accused of software piracy by Conitec in the process.
Re: Wierd invalid pointer. [Re: draculaFactory] #340293
09/02/10 22:07
09/02/10 22:07
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
mouse_panel doesnt 'work' for texts, BUT if there is a text with a higher layer
than an underlying panel, then the text will 'cover' the panel, and
mouse_panel will become NULL.

I hate 'dirty' if's, so thy this if instead...
if((temp_panel.layer > panel.layer) && (temp_panel != panel))
just on a chance the two parts of the if are getting tangled...

Or try this one, in case its temp_panel going wrong...
Code:
function sort_panels(PANEL* panel)
{
	var list_no = 0;
	var sss = 0;
	
	PANEL* temp_panel;
	
	while(list_no < total_panels)
	{
		temp_panel = ptr_for_handle(panel_list[list_no]);
		if((temp_panel!=NULL) && (panel!=NULL))
		{	if((temp_panel.layer > panel.layer) && (temp_panel != panel))
			{
				layer_sort(temp_panel,temp_panel.layer - 1);
			}
		}
		list_no += 1;
	}
	
	layer_sort(panel,total_panels);
}

But beware, this may just "hide" your real problem...
But if it does, that should help you FIND the real problem...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Wierd invalid pointer. [Re: EvilSOB] #340297
09/02/10 22:54
09/02/10 22:54
Joined: Feb 2005
Posts: 647
Williamsburg, VA USA
draculaFactory Offline OP
User
draculaFactory  Offline OP
User

Joined: Feb 2005
Posts: 647
Williamsburg, VA USA
Well there is no invalid pointer crash with the code that you posted, but it caused a new problem: the panels are sorted in reverse order. Clicking on a panel now sorts it to the bottom layer lol.


Making dreams come true... And being erroneously accused of software piracy by Conitec in the process.
Re: Wierd invalid pointer. [Re: draculaFactory] #340310
09/03/10 02:58
09/03/10 02:58
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Do you have, or can you make, a stand-alone mini-project we can test this with?


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Wierd invalid pointer. [Re: EvilSOB] #340352
09/03/10 11:57
09/03/10 11:57
Joined: Feb 2005
Posts: 647
Williamsburg, VA USA
draculaFactory Offline OP
User
draculaFactory  Offline OP
User

Joined: Feb 2005
Posts: 647
Williamsburg, VA USA


Making dreams come true... And being erroneously accused of software piracy by Conitec in the process.
Page 1 of 3 1 2 3

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1