Posted: 15th Jul 2023 20:20
Hello everyone, I'm currently using the function: agk::GetStringToken in my code and I noticed some "strange" behavior, if I keep agk::GetStringToken inside a loop waiting for the user's choice, the use of ram memory only increases
(already got to the point of starting with 50mb and getting 400mb of constant ram usage)...
Is there a way to make this more optimized?

I don't know how I should treat this, because I'm starting with c++ for now...

Does anyone have any idea what could be going on?
Posted: 15th Jul 2023 22:30
moved from Showcase to Classic chat... (i wonder why Tier 2 chat is no longer available?).
Posted: 16th Jul 2023 2:41
I seem to recall that anytime a Tier 2 AppGameKit function returned a string you had to free/release/destroy it when you finished using it

This is a Note from another function (StripString());
"Note that if you are calling this command from tier 2 then the returned string must be deleted when you are done with it."
Posted: 16th Jul 2023 3:07
would you have any example of how to " free/release/destroy " correctly?

I tried using
+ Code Snippet
 
 const char* mystringvar = agk::GetStringToken(CharInfoInput,";", i )
delete[] mystringvar; 

but it still keeps increasing the memory of the application consecutively
Posted: 16th Jul 2023 3:14
DeleteString must be called when done with char* that is returned from agk's functions. This is the message a lot of the functions show in the help pages "If you use this command in tier 2 you must delete the returned string with DeleteString when you are finished with it. "
Posted: 17th Jul 2023 0:40
I understand, but what would be a practical example in C++?
I'm very new to C++ so I have no idea how to do it correctly?
Posted: 17th Jul 2023 6:09
The following example is taken from the help site and converted to c++.
It was just a quick thing I ported for example. More thought should go into what you want to do.
But it compiled and ran and did what it was supposed to do. just keep in mind it just useless example code.
Honestly I've been utilitizing AGK's uString class for strings so when I call a function that returns a char*. I basically
use a temp string used to hold the returned char* and then save that to a uString object. I believe the uString allocates its own memory
and copies the contents to a new char* internal and when the uString object goes out of scope it automatically de-allocates the memory used.
Once the returns char* is save to the uString object I call agk:eleteString(tmpstr) to de-allocate the memory the library allocates.

char* example:

+ Code Snippet
// Includes
#include "template.h"

// Namespace
using namespace AGK;

app App;

char* word;

void app::Begin(void)
{
	agk::SetVirtualResolution (1024, 768);
	agk::SetClearColor( 151,170,204 ); // light blue
	agk::SetSyncRate(60,0);
	agk::SetScissor(0,0,0,0);
	
	char* text = "AGK,Example:Text";
	word = agk::GetStringToken(text, ",:", 2);
	agk::ResetTimer();
}

int app::Loop (void)
{
	if (agk::Timer() > 5) return 1;
	agk::Print( agk::ScreenFPS() );
	agk::Print("Word (token) number to is: ");
	agk::Print(word);

	agk::Sync();

	return 0; // return 1 to close app
}


void app::End (void)
{
	agk::DeleteString(word);
	//delete[] word;
	
}


uString Example.

+ Code Snippet
// Includes
#include "template.h"

// Namespace
using namespace AGK;

app App;

uString word;

void app::Begin(void)
{
	agk::SetVirtualResolution (1024, 768);
	agk::SetClearColor( 151,170,204 ); // light blue
	agk::SetSyncRate(60,0);
	agk::SetScissor(0,0,0,0);
	
	uString text = "AGK,Example:Text";
	char* tmp;
	tmp = agk::GetStringToken(text, ",:", 2);
	word = tmp;
	agk::DeleteString(tmp);
	agk::ResetTimer();
}

int app::Loop (void)
{
	if (agk::Timer() > 5) return 1;
	agk::Print( agk::ScreenFPS() );
	agk::Print("Word (token) number to is: ");
	agk::Print(word);

	agk::Sync();

	return 0; // return 1 to close app
}


void app::End (void)
{
	
}
Posted: 17th Jul 2023 23:35
thanks it works .