2 registered members (AndrewAMD, Nymphodora),
972
guests, and 8
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
malloc & nxalloc
#472509
05/02/18 11:00
05/02/18 11:00
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
OP
Senior Expert
|
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:
#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).
|
|
|
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
Expert
|
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.
|
|
|
Re: malloc & nxalloc
[Re: alibaba]
#472515
05/02/18 12:13
05/02/18 12:13
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
OP
Senior Expert
|
OP
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Yeah, I know that. As the manual says: 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?
|
|
|
|