Lite Foundation (now with beginner support)

Posted By: WretchedSid

Lite Foundation (now with beginner support) - 10/03/10 18:41

My first topic about this project was maybe a little bit too deterrently for everyone, I didn't described the project but threw a bunch of code files at you.
I can understand that this is not a good idea, so I decided to create this new topic.

So what is Lite Foundation?
Lite Foundation is a library to make your life as programmer easier. It provides you with an foundation for your project and helps you to reduce a lot of boilerplate code (boilerplate code is code that you need to write over and over again for all your projects). It contains functions and objects that help you to work with dynamic arrays, binary data, timers and moar.
It is also easy extendable so you can add your custom types that profits from the Lite Foundation magic.
The magic of Lite Foundation is described completely in the getting started pdf file that is included with this release. So if you didn't knew where to start with Lite Foundation and/or what you could do with it, there is a document for you.
Together with the getting started documentation is a full reference for every object and function and there are also 11 examples that shows you how to use Lite Foundation correctly.

You can grab Lite Foundation for free here: http://cl.ly/3e2872905214e7607d62
Its released under the MIT license which allows to do everything what you want with it.

If you still have questions or if something is a little bit unclear, don't hesitate to ask them, I will answer every question!

Thanks to Hummel, Michael Schwarz and Puppeteer for their tips with the getting started document!
Posted By: Saturnus

Re: Lite Foundation (now with beginner support) - 10/11/10 23:59

Nice!

It leaves the impression of an elaborate and well structured library. The getting started document was a good idea, too.

I didn't have a deeper look into it yet, though. I certainly will just as I need it.

BTW: The issue of having to pass at least one parameter when calling struct methods seems to be fixed in A8. At least the compiler doesn't like _observer->callback(0) etc. and won't do its job anymore. In A7 it compiles fine.
Posted By: WretchedSid

Re: Lite Foundation (now with beginner support) - 10/21/10 14:56

Hey, thanks for the nice feedback.
Yes, A8.03 indeed fixed this bug, previous A8 versions didn't had it. As there is no define that indicates the A8/A7 version, I had to rewrite the callback system to make it work with A8 and A7.

I also did some other minor changes, so I decided to release a new version (btw, thanks to the 24 downloaders, although I wished some more feedback :P).
The new version has the version number 0.4.1:1025 and makes the following changes:
-> The LFTypeRef has now two new members that are used to identify objects as Lite Foundation objects (including their version). The first member just holds the size of the struct (to identify changes) and the second member is the magic number (22574592 >> 8) used to identify an Lite Foundation object
-> Together with the new members comes a new function to check if an object is an Lite Foundation object or not (LFIsType()).
-> This comes to the next great thing: LFArrayRef objects can now also hold non Lite Foundation objects, although they won't get archived and don't retained.
-> Another thing is the LFTypeCopy() function that creates a copy of Lite Foundation objects (when they implement it), you can also adopt this by overwriting the copy member of your LFTypeRef instance (A more detailed guide will follow). The Lite Foundation objects that implement this create deep copies, you may adopt this behavior if you want.
-> When calling LFAutorelease() the objects gets released within the next step of the run loop. So far so known, new is the following: They get released at the beginning of the step, not at the end. This means that when you call LFAutorelease() from within a LFTimerRef or LFRunLoopObserverRef callback, the autorelease objects don't receive a release call until the next step.
-> The already mentioned bug with A8.03 is fixed. LFRunLoopObserverRef and LFTimerRef callbacks now need to have one argument from type LFTypeRef. They receive the pointer to the LFTimerRef or LFRunLoopObserverRef object that called the callback.

Sooo, thats it, like I said, this is just a small update.
Here is the download link: http://cl.ly/b93cf41fe8777a718139

What follows next: I want to extend the current function set and also cleanup everything a bit. The LFBucketRef needs also to be finished (what a damn beast) and I have a few other ideas for objects (anyone interested in an object that provides non blocking IO?). I also want to write two new guides, the first one is a "Memory Management Guideline" (I already started it) and the second one will be a guide on how integrate Lite Foundation into your existing or new application.

Every kind of feedback is really appreciated. Really. I won't eat you!
Posted By: Superku

Re: Lite Foundation (now with beginner support) - 11/02/10 00:34

When running the demo_timer.c file (version: 21.10.2010 14:35), the application runs for about 5 seconds but crashes with the following error message:
"Error E1513: Script crash in createObject: SYS"
Posted By: WretchedSid

Re: Lite Foundation (now with beginner support) - 11/02/10 08:02

Thats strange, I can't run it atm but for me it looks perfectly fine.
The demo is supposed to create two timers, one that displays the current time (fires every second) and one that spawns an object (fires every five seconds).
The second function to spawn the object is completely based on top of Gamestudio and does nothing Lite Foundation specific, so I assume that this crash isn't Lite Foundations fault.
But I really can't see anything wrong, maybe someone else can see whats going wrong?
Code:
void createObject(LFTimerRef *timer)
{
	ENTITY *object = ent_create("box.mdl", vector(2048, 0, 0), NULL);
	var force = 32;
	
	while(object.z > -150) 
	{
		object.z += force * time_step;
		force -= (4 * time_step);
		
		wait(1);
	}
	
	set(object, TRANSLUCENT);
	object.alpha = 100;
	
	while(object.alpha > 0)
	{
		object.alpha -= (18 * time_step);
		object.z += force * time_step;
		force -= (4 * time_step);
		
		wait(1);
	}
	
	ent_remove(object);
}


(demo_timer.c line 28)


On a side note: I'm still working on the project, currently I'm rewriting the LFDateRef object that allows you now to have milliseconds (on *nix machines even nanoseconds), this also affects every object that works with dates, eg. the timer that can now fire in a millisecond range.
I have implemented an object that allows you to serialize and deserialize any kind of object, without the problem that the data breaks once you change your objects internal structure. It also don't has problems when you switch your byte order or the size of the objects (eg. when switching between x86 and x86-64).
I'm currently working on an object that allows you to serialize and deserialize a complete list of objects.
Oh, and a unit testing object comes too...
Posted By: Superku

Re: Lite Foundation (now with beginner support) - 11/02/10 10:41

"The demo is supposed to create two timers, one that displays the current time (fires every second) and one that spawns an object (fires every five seconds)."

Yes, I've already seen that. The first timer seems to run fine, but no entity gets created. I've used sys_marker and the error message shows "ABC" now, but I have no idea why it crashes.

Code:
void createObject(LFTimerRef *timer)
{
	sys_marker("ABC");
	ENTITY *object = ent_create("box.mdl", vector(2048, 0, 0), NULL);
	var force = 32;
	sys_marker(NULL);

	while(object.z > -150) 
	{
	...



Btw your Lite Foundation is a very neat contribution, I think I'm gonna use it for my next project. Looks like it was a lot of work, too.
Your file-write command seems to use some kind of encryption or am I'm wrong?
EDIT: The reference with module/ structure list is nice.
Posted By: WretchedSid

Re: Lite Foundation (now with beginner support) - 11/02/10 11:54

I just tried to reproduce the crash on a laptop running Windows 7 with Gamestudio A8.03.2 and the demo didn't crashed.
Maybe someone else can test it too and report if its works or not? This looks a little bit like a Gamestudio related problem and I can't see any reason in my code that could produce the bug.

Quote:

Your file-write command seems to use some kind of encryption or am I'm wrong?

I guess you mean the files produced by LFDataRef objects? If so, no, they aren't encrypted, but the LFDataRef object is meant to store binary data and thus also saves binary data.
If you add a CString to an LFDataRef object and then write it to a file, you will see that it stores the data unencrypted (btw, the LFDataRef and LFStringRef objects are toll-free bridged in the next version)
Posted By: Superku

Re: Lite Foundation (now with beginner support) - 11/02/10 12:20

"[...]Windows 7 with Gamestudio A8.03.2 and the demo didn't crashed."

Hm that's weird. I've tried the code with the current beta (8.04.2b) and with 7.86, same crashes. I'm using Windows XP, if that makes any difference.
Posted By: WretchedSid

Re: Lite Foundation (now with beginner support) - 11/02/10 13:21

Thats indeed very weird. I would expect such crashes in an multithreaded application, but I guess that you didn't modified the run loop so that it runs in a background thread.

I also don't see a reason why it should crash on Windows XP, I will test it on another machine later this day (A7 and A8, Windows XP), but it would be also awesome if someone else could test it too.
Posted By: Quad

Re: Lite Foundation (now with beginner support) - 11/02/10 13:56

win7 x64 8.03.2 works fine. will test on xp

and also, i just figured how awesome the lite foundation is.
Posted By: Bunsen

Re: Lite Foundation (now with beginner support) - 11/02/10 19:45

I don't beleave that the error is caused by Lite-C (with different WINDOWS platforms).

You should verify this function call:

Code:
void timerCallback(LFTimerRef *timer)
{
   ...
   LFRelease(dateText); // <-- Exception Code 0xC0000005 (Protected memory access)
}



I hope it will help.
Posted By: WretchedSid

Re: Lite Foundation (now with beginner support) - 11/02/10 19:49

Okay, I forced some other users to test it too (sadly no XP user), no one could confirm the crash (Nicotin Win 7, Michael_Schwarz Win 7, Hummel Win Vista).
I also tested it here on another PC running Windows XP but I still can't reproduce the crash.

Edit: @Bunsen, I don't see why the code should crash. It just frees some previous allocated memory and removes the dateText object from the current run loop.
I recreated the function here and went step by step through the code via LLDB and there is no illegal access =/
(Btw, Superku said that the crash occurs in the other timer callback and not in the one that fires every second)
Posted By: Superku

Re: Lite Foundation (now with beginner support) - 11/02/10 20:09


Code:
void createObject(LFTimerRef *timer)
{
	sys_marker("ABC");
	ENTITY *object = ent_create(CUBE_MDL, vector(2048, 0, 0), NULL); // "box.mdl"
	sys_marker(NULL);
	var force = 32;
	...

void main()
{
	level_load(NULL); 
	sys_marker("BBC");
	ENTITY *object = ent_create(CUBE_MDL, vector(2048, 0, 0), NULL); // "box.mdl"
	sys_marker(NULL);
	...



There is no error in the main function, only the ent_create-command in createObject fails.
Posted By: Bunsen

Re: Lite Foundation (now with beginner support) - 11/02/10 20:11

I'm using WINDOWS XP and it crashes.
You might run your program over night (or at least for maybe 2 hours).
Posted By: WretchedSid

Re: Lite Foundation (now with beginner support) - 11/02/10 20:41

Bunsen, thank you very, very much! I managed to reproduce the bug, the problem is that I free a non allocated memory area.
You are right, LFRelease(dateText) does some bogus things that it shouldn't do.

What I don't know is why it does them, it looks like the dateText object somehow loses its retain count without an obvious reason. I will edit the post once I understand this completely, in the meantime: Stay away from LFDescription() (You can remove the very first timer to see the full demo (hopefully))
Posted By: WretchedSid

Re: Lite Foundation (now with beginner support) - 11/02/10 21:09

Edit: Okay, I'm stupid as hell. This is expected behavior, I was just too stupid to see it.
The problem is inside the LFRelease() function, it removes the object first from the run loop which releases the object again after it retained it, setting its retain count back to zero.
This works in all other cases because it leaves the retain count untouched, however it fails in this case for obvious reasons. Here is a working version of the LFRelease() function:
Code:
LFTypeRef *LFRelease(LFTypeRef *ref)
{
	if(ref->type == LFTYPE_ZOMBIE)
	{
		printf("Releasing already deallocated object %p!\n", ref);
		return NULL;
	}
	
	ref->retainCount -= 1;
	
	if(ref->retainCount == 0)
	{
		if(ref->type == LFTYPE_RUNLOOP)
		{
			LFRunLoop *runLoop = (LFRunLoop *)ref->data;
			
			if(runLoop->running) // A running runloop can't be deallocated!
			{
				runLoop->dirty		= 1;
				runLoop->shouldStop = 1;
				
				ref->retainCount ++;
				return ref;
			}
		}
		
		if(ref->runLoop && LFRunLoopCurrentRunLoop())
		{
			ref->retainCount = 2;
			LFRunLoopRemoveObject(LFRunLoopCurrentRunLoop(), ref);
			ref->retainCount = 0;
		}
		
		if(ref->pool)
		{
			LFAutoreleasePoolRef *_pool = (LFAutoreleasePoolRef *)ref->pool;
			
			ref->pool = NULL;
			ref->retainCount = 2; // Avoid double realeasing when removing the object from the array
			
		
			LFAutoreleasePool *pool = (LFAutoreleasePool *)_pool->data;
			LFArrayRemoveObject(pool->objects, ref);
			
			ref->retainCount = 0;
		}
		
		if(ref->dealloc)
			ref->dealloc(ref);
			
		if(ref->data)
			free(ref->data);
		
		if(!LFZombieFactory)
			free(ref);
		else
			ref->type = LFTYPE_ZOMBIE;
		
		return NULL;
	}
	
	return ref;
}



Please remark: I won't update the current version, you have to copy the patch manually as I'm almost finished with the next version (which will of course include the patch)!
Thanks superku and bunsen!

Edit: Whoops, this shouldn't be a double post. Sorry, clicked the wrong button. Can a mod please merge the two posts?
© 2024 lite-C Forums