Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (7th_zorro, Quad, VoroneTZ, 1 invisible), 623 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Script crash with array of engine objects after wait(1) #442536
06/24/14 21:31
06/24/14 21:31
Joined: Jun 2013
Posts: 108
Alberta, Canada
C
CanadianDavid Offline OP
Member
CanadianDavid  Offline OP
Member
C

Joined: Jun 2013
Posts: 108
Alberta, Canada
Can anyone explain why the following simple program crashes after the TEXT* objects' first strings are printed for the first time?

Code:
#include <acknex.h>
#include <default.c>

TEXT** makeTexts() {
	TEXT* array[2];
	array[0] = txt_create(10, 1);
	array[1] = txt_create(10, 1);
	
	int i;
	for(i=0; i<10; i++){
		str_cpy((array[0].pstring)[i], "Text1");
		str_cpy((array[1].pstring)[i], "Text2");
	}
	
	return array;
}

function printMessages(TEXT** array){
	printf("%s", _chr((array[0].pstring)[0]));
	printf("%s", _chr((array[1].pstring)[0]));
}

function main() {
	TEXT** array = makeTexts();
	while(1){
		printMessages(array);
		wait(1);
	}
}



The code works fine when it is outside the while loop; the code works until a wait(1) is encountered.

Re: Script crash with array of engine objects after wait(1) [Re: CanadianDavid] #442537
06/24/14 21:45
06/24/14 21:45
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
There is no problem with the engine objects. The array is a local variable only valid while the function is used. You need to allocate the array to be able to use it out of the function.

Code:
TEXT **array = sys_malloc ( sizeof(TEXT*) * 2 );


Re: Script crash with array of engine objects after wait(1) [Re: txesmi] #442538
06/24/14 22:16
06/24/14 22:16
Joined: Jun 2013
Posts: 108
Alberta, Canada
C
CanadianDavid Offline OP
Member
CanadianDavid  Offline OP
Member
C

Joined: Jun 2013
Posts: 108
Alberta, Canada
Great thanks for the clarification!

Re: Script crash with array of engine objects after wait(1) [Re: CanadianDavid] #442539
06/24/14 22:38
06/24/14 22:38
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
glad to help grin

don't forget to remove all the created stuff at the end.

Code:
void removeTexts ( TEXT **array )
{
   int i = 0, ii = 0;
   for ( i=0; i<2; i+=1 )
   {
      for ( ii=0; ii<array[i].strings; ii+=1 )
         str_remove ( (array[i].pstring)[ii] );
      ptr_remove ( array[i] );
   }
   sys_free ( array );
}


Re: Script crash with array of engine objects after wait(1) [Re: txesmi] #442540
06/24/14 22:45
06/24/14 22:45
Joined: Jun 2013
Posts: 108
Alberta, Canada
C
CanadianDavid Offline OP
Member
CanadianDavid  Offline OP
Member
C

Joined: Jun 2013
Posts: 108
Alberta, Canada
You're right I almost forgot! Thanks again for your help!


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