Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Nymphodora), 972 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
malloc & nxalloc #472509
05/02/18 11:00
05/02/18 11:00
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Hi guys!

As I'm trying to get more used to structures, I've faced weird malfunction W1516.. Invalid memory area test ( test - function's name ).
For me code seems fine, but I still can't get it. Can you please poke me in a right direction? Thank you.

Here is a code:
Code:
#include <acknex.h>
#include <default.c>

#define PRAGMA_POINTER

var test(ENTITY *actor);

typedef struct RD {
	
	var id;	
	VECTOR vec;
	ENTITY *ent;
	
} RD;

void npc_event(){
	
	if(event_type == EVENT_CLICK){
		
		my->emask &= ~ENABLE_CLICK;
		my->event = NULL;
		
		test(my);
		
	}
	
}

void npc(){
	
	vec_set(&my->scale_x, vector(1, 1, 2));
	c_setminmax(my);
	set(my, TRANSLUCENT);
	my->emask |= ENABLE_CLICK;
	my->event = npc_event;
	
}

void test(ENTITY *actor){
	
	//RD *example = sys_malloc(sizeof(RD)); // randomly
	RD *example = sys_nxalloc(sizeof(RD)); // constantly
	
	example->id = 2;	
	vec_set(&example->vec, vector(-5, 5, 0));	
	example->ent = ent_create(CUBE_MDL, &actor->x, NULL);
	
	var counter = 3;
	
	while(actor){
		
		counter -= time_frame / 16;
		if(counter <= 0){ break; }
		
		wait(1);
		
	}
	
	if(example->ent){
		
		safe_remove(example->ent);
		
	}
	
	sys_free(example);
	example = NULL;	
	
	my->emask |= ENABLE_CLICK;
	my->event = npc_event;
	
}

void main(){
	
	video_set(800, 600, 16, 2);
	video_aspect = 1.333;

	d3d_lines = 1;
	warn_level = 6;
	fps_max = 60;
	
	level_load("");
	wait(3);
	
	vec_set(&camera->x, vector(342, 0, 140));
	vec_set(&camera->pan, vector(180, -13, 0));
	
	mouse_mode = 4;
	mouse_pointer = 2;
	
	ENTITY* ground_ent = ent_createterrain(NULL, nullvector, 100, 100, 1000);
	ent_setskin(ground_ent, bmap_fill(bmap_createblack(32, 32, 16), COLOR_GREEN, 100), 1);
	
	ent_create(CUBE_MDL, vector(0, 0, 32), npc);

}

I tried both sys_malloc and sys_nxalloc, but was getting W1516 from both (somehow malloc is hard to reproduce, but I remember getting malfunction from it too).


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: malloc & nxalloc [Re: 3run] #472511
05/02/18 12:02
05/02/18 12:02
Joined: May 2008
Posts: 2,113
NRW/Germany
alibaba Offline
Expert
alibaba  Offline
Expert

Joined: May 2008
Posts: 2,113
NRW/Germany
Maybe because of your prototype "var test(ENTITY *actor);"? Because in the code you use "void test(ENTITY *actor)"
Nevermind

Last edited by alibaba; 05/02/18 12:06.

Professional Edition
A8.47.1
--------------------
http://www.yueklet.de
Re: malloc & nxalloc [Re: alibaba] #472512
05/02/18 12:05
05/02/18 12:05
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Hi man!

No, it's not cause of that. In original code, I don't have any prototypes, it was just used in this example. And even if I would change this example so it won't use any prototypes, malfunction is still there.


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: malloc & nxalloc [Re: 3run] #472513
05/02/18 12:08
05/02/18 12:08
Joined: May 2008
Posts: 2,113
NRW/Germany
alibaba Offline
Expert
alibaba  Offline
Expert

Joined: May 2008
Posts: 2,113
NRW/Germany
Okay, but according to the manual, you shouldn't use any ent_create functions in an event. There may be a problem.


Professional Edition
A8.47.1
--------------------
http://www.yueklet.de
Re: malloc & nxalloc [Re: alibaba] #472515
05/02/18 12:13
05/02/18 12:13
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Yeah, I know that. As the manual says:
Quote:
Event functions are actually executed during the engine function that caused the event, like a c_move, c_scan, or c_trace instruction of another entity. The event function itself should be simple. It normally should only transfer information to the entities' main function - it shouldn't perform instructions that can trigger events itself, displace entities, start particles or change anything else in the level. Thus instructions like c_move, ent_create, ptr_remove, c_trace etc. must not be performed. If the event function must perform such 'critical instructions', precede them by a wait(1) for delaying them to the next frame. Then it's safe.
I guess that's cause those c_ functions and ent_create as well, toy around with 'you' pointer. Which is also used in events. That's why it requires wait(1) in order to 'seperate' pointers from eachother. But I've done c_traces, c_moves, ent_creates before in events without facing any troubles (using seperate function and wait(1) before does work pretty well, if I would add 'wait' in test function it won't help anyway), so I guess it's not related to that here either. It's probably has to do with the way I used structure, maybe I've done something wrong?


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: malloc & nxalloc [Re: 3run] #472516
05/02/18 12:25
05/02/18 12:25
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Don't use sys_free with sys_nxalloc (or malloc). sys_free only works with sys_malloc. Personally, I only ever use sys_malloc.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: malloc & nxalloc [Re: Superku] #472517
05/02/18 12:30
05/02/18 12:30
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline OP
Senior Expert
3run  Offline OP
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Thank you Superku! I can't reproduce malfunction with sys_malloc right now, and I'm not really sure any more in fact that was getting malfunction with it yesterday, maybe I was too tired..

Thank you all guys, I'll make some tests, and if it won't work, will post again.


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung

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