Gamestudio Links
Zorro Links
Newest Posts
What are you working on?
by 3run. 02/28/20 09:28
Editting
by peterhw. 02/27/20 15:20
PS1 texturing bubbling ?
by 3run. 02/27/20 13:36
enum case / if else
by danatrader. 02/27/20 00:07
AUM Magazine
Latest Screens
The Space Between
Pogostuck: Rage With Your Friends
Worst Case Z
AckCon'18 - Lotter vs the World 2 - Preview Release
Who's Online Now
9 registered members (3run, AndrewAMD, Zheka, FLD, Dalla, jenGs, smamax, Iglarion, txesmi), 963 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Emrah06, zac, DebraSteed, Ricard, Apollo1
18379 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,293
Hiporope and its pain
txesmi Online
Serious User
txesmi  Online
Serious User

Joined: Jun 2007
Posts: 1,293
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,293
Hiporope and its pain
txesmi Online
Serious User
txesmi  Online
Serious User

Joined: Jun 2007
Posts: 1,293
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