PDA

View Full Version : Delphi skipping lines during run..



M109uk
13-03-2005, 12:53 AM
Hi all,

I have a problem, i dont know if its to do with delphi or my app, but its not the first time i had the problem..

basiclly i have some code in my apps initialization:


procedure InitXShell;
begin
Clear world;
Load world from file;
Set Camera;
..
..
end;

procedure TxsWorld.LoadFromFile(const Filename: String);
begin
Create Stream
Check Header
Clear textures + shaders
Load world data
Free stream
end;


My problem is while i run my app it goes to load the world from a file, creates the stream and checks the header then it goes to the end of the function (Free stream), then to another function (my finalization function) then runs several other functions and then goes back to my world loading function and trys to load the worlds data, but now of course the stream and everything has been freed and causes an error..

Does any one else have a similar problem? if so how can i stop this from happening?

Thanx for any help

technomage
13-03-2005, 07:28 PM
You can get wierd results like this if an error occurs early in your LoadFromFile method. I would put a few exception handlers in and see if you get any errors. It sounds like something might be going wrong in the read header section.

On the few occasions I have seen something similar, it's usually when I'm doing something with Pointers or reading from stream into a buffer and somethings goes wrong (buffer overrun or the variable I'm passing in is incorrect).

Hope this helps

Dean

M109uk
13-03-2005, 11:30 PM
Thanx for the reply,

iv had a look through my functions but cant see anything wrong, delphi wont let me put and exceptions actually in the load function for some reason, it just ignours them when i run the app.

I use the same functions in another app and the same file but i dont have these problems...

i'l try and get more information when i get back home..

cairnswm
14-03-2005, 05:23 AM
Check that you dont have another function with the same name in another file somewhere. From your explanation it sounds as though the functions you are looking at are not the ones being called.

Try put a break point BEFORE the function is called and the step into the function to make sure it is the right one being called.

M109uk
14-03-2005, 09:12 AM
Hi,

The correct function is being called, its just half way through loading my world file it just goes to the end of the worlds main loadfromfile function to early, below is a more complete code:


procedure XShellLoadDefaultWorld;
begin
Log(PChar('loading default world..'));
World.LoadFromFile(XShellDataDir+'defaultworld.xsw ');
SetupDefaultSpawn;
SetupActor;
end;

procedure TxseWorld.LoadFromFile(const Filename: String);
var
Stream: TxnStream;
i,Cnt: Integer;
oClass: String;
begin
Stream := TxnStream.Create(Filename, xmOpenRead);
If ValidHeader(Stream, WORLD_HEADER) Then
Begin
Clear;
Worldspawn.LoadFromStream(Stream);
Stream.ReadInteger(Cnt);
For i := 0 To Cnt-1 Do
Begin
Stream.ReadString(oClass);
Objects.Add('', oClass);
Objects[i].LoadFromStream(Stream);
End;
End;
Stream.Free;
end;

function TxseObjects.Add(const Name,Classname: String): Integer;
var
nObject: TxseBaseObject;
begin
Result := -1;
If fOwner = Nil Then
Begin
If Not xseObjectRegister.IsAllowed('', Classname) Then Exit;
End Else
Begin
If Not xseObjectRegsiter.IsAllowed(fOwner.oClassname, Classname) Then Exit;
End;
nObject := xseObjectRegister.GetClass(Classname).Create(Self) ;
If nObject = Nil Then Exit;
nObject.Name := Name;
nObject.oClassname := Classname;
Result := inherited Add(nObject);
nObject.Index := Result;
end;

procedure TxseBaseObject.LoadFromStream(const Stream: TxnStream);
var
i,Cnt,Index: Integer;
oClass: String;
begin
Stream.ReadString(Name);
Stream.ReadInteger(Cnt);
If Children <> Nil Then
Begin
Children.Clear;
For i := 0 To Cnt-1 Do
Begin
Stream.ReadString(oClass);
Children.Add('', oClass);
Children[i].LoadFromStream(Stream);
End;
End;
end;

begin
//main application code
while GameRunning Do
Begin
//code
End;
// finalize code
end.


The xseObjectRegister is a list of all my world class types, such as groups, polygons, faces, particle systems, lights, etc..

Both Objects and Children are TxseObjectList class types (which is derived from TList), which are a list of TxseBaseObject.

In the world.loadfromfile procedure the app runs in to the for statement and starts to load the objects, the first object has 11 children some with a child, when it trys to load the 5th child it then exits this procedure and goes back into world.loadfromfile to stream.free, then goes to the end of my applications code where the //finalize code is and then goes back to where it was in the objects loadfromstream procedure, but by this time it crashes because it trys to read something from the stream, but it has been released :s

I have renamed all the procedures to check see if they are being called some where else but this isnt happening.. :s

savage
14-03-2005, 09:42 AM
I would suggest getting rid of any intermediary files like *.dcu and doing a "Build All". Also make sure that you have exceptions turned *on* when debugging your code as it sounds like an AV may be happening in that function and it then throws you out. The only other possiblity that comes to mind would relate to multi-threading. Are you using anything in another thread that may be causing a problem?

M109uk
14-03-2005, 06:44 PM
hi,

i have tried what you suggested, i deleted ALL my sources dcu files and then re-built my app, how ever the same problem occours..
I have also checked my project exception options and they are all turned on except the bottom 3 (unsafe type, unsafe code and unsafe typecast) i turned these on and it shows warnings for everything thats uses pointers by the looks of things (including dglOpenGL.pas), over 7000 warnings so far and still hasnt got to my code yet :s

I have added my load function into a while statement to check if it is a problem with the load function being called more than once but still same problem, and the function isnt being called several times :s

Urm im not using any threads at the moment in my app..

If anyones intrested and willing, i could email the source and see if the problems are at my end or not?!?

M109uk
16-03-2005, 03:39 PM
hmm,
i got around to having another play with it and decided to comment out my world loading code, but the same problem comes up so of course it cant be my world file or loading code...

i guess i'l just have to go line by line now :s

thanx for your help though guys..

JSoftware
16-03-2005, 04:39 PM
i saw you were reading a string from your own steam class

you do remember not to read a string of undefined length?

M109uk
16-03-2005, 04:46 PM
Iv managed to round it down to 3 functions that the problem is in, think i know what it could be now :)


Yeah my stream class does read and write a string,
its not that cause i use it in all my class types,

when i write a string, say 'SAMPLE' then it writes each character and then adds #0 to then end, when i read it it just reads a character at a time until it reaches the #0 character..

source (TxnStream is based on TFileStream):

procedure TxnStream.WriteChar(const Value: Char);
begin
Write(Value, SizeOf(Char));
end;

procedure TxnStream.ReadChar(var Value: Char);
begin
Read(Value, SizeOf(Char));
end;

procedure TxnStream.WriteString(const Value: String);
var
i: Integer;
begin
For i := 1 To Length(Value) Do WriteChar(Value[i]);
WriteChar(#0);
end;

procedure TxnStream.ReadString(var Value: String);
var
c: Char;
begin
Value := '';
Repeat
ReadChar(c);
If c <> #0 Then Value := Concat(Value, c);
Until c = #0;
end;

Robert Kosek
16-03-2005, 06:49 PM
I think it's the read/write string part. Would you try this code instead? I know it works, and it'll narrow down your problem possibilities:

procedure TxnStream.WriteString(const Value: String);
var
l: integer;
begin
l := length(Value);
write(l,sizeof(l));
write(pchar(Value)^,l);
end;

procedure TxnStream.ReadString(var Value: String);
var
l: integer;
begin
read(l,sizeof(l));
setlength(Value,l);
read(pchar(Value)^,l);
end;

I think that the loop that looks for the #0 character is what's killing your code. On top of that, it could be error prone due to the char by char read/write... I'm no pro though.

M109uk
16-03-2005, 07:25 PM
Hi Robert,

I will give your code a try, it may solve a minor problem im having with stringlists,

I have finally discovered what was causing my problem,
simply because i was stupid and forgot to register a classtype that my world structure uses :s

My world structure contains several classes depending on each object (for example a face would be TxseFace, a lightsource would be TxseLightsource, etc) but all these are stored in a global register and every time i add or load an object in to the world structure it checks to see if it is valid in the register and can then register it.

But for this to work at the moment i have to have each classes unit in the uses clause of the applications project file or main unit so because i have in the classes unit at the initialization section i register the class, this is probably really dump, and i will have to change this at some time :s

Robert Kosek
16-03-2005, 08:32 PM
That'll kill you every time... :roll: Been there, done that...

My code is really what I learned elsewhere, and I found it to not only be fast but error free. Since your strings won't likely be 2,147,483,647 characters long I would reccommend using a "word" variable, and it'll be smaller as well.


I've checked out your site m109uk, and seeing your kind of issues with flat files why don't you check this site out: FreePgs (http://www.freepgs.com/)

A one time fee of $16 will get you 350mb of space and unmetered bandwidth (50gb a month or so) + Perl/PHP/MySQL and more + an annual $10 for a domain (optional). If you already have a domain, you could talk to the admin and get it to manage that domain. I've found it to be a very nice hosting plan, it's a subsidiary of LCVS.net (http://www.lcvs.net/).

M109uk
16-03-2005, 11:13 PM
lol yeah tell me about it, its always something :s
Most of my code goes on the principle of future core updates, and on configuration which isnt always the best mix :d

one of these days i will learn haha


Cool thanx i will have a look,
the bigger problem with my current server is although i have so called "unlimited" space i have to connect to the webspace and email clients through there 32bps connection (gets very annoying quickly) :(

Robert Kosek
17-03-2005, 04:23 PM
Quick note on the hosting I reccommended, but it's $10 for the backend for the domain .... and it must be registered seperately, so it end's up at about $18.95 a year for a site. But that's not bad. Sorry it's in dollars, I'm not that familiar with Pounds and the UK monitary system.

M109uk
17-03-2005, 06:48 PM
lol thanks Robert,
yeah i have registered an account and the domain name, a grand total of around 30$ which is probably around 18AĴ£ which is great :)
and didnt take very long to upload everything :D

just waiting for the domain name to be transfered (48hrs i think), got some more updates to do on my website now (again) lol from files to dbs...

thanx again Robert..

Robert Kosek
17-03-2005, 07:11 PM
No problem, glad to help! My site's under construction as I just got my domain and have to upload/configure everything. I'll pm you a link once it's operational.

M109uk
17-03-2005, 10:40 PM
Cool, i look forward to seeing it :)



Iv been bored for the last few hours, and decided to go from BioCoders to BioSphere :s
and even designed a logo kinda thingy:
http://www.freepgs.com/m109uk/bioshere_logo_0000.bmp (http://www.freepgs.com/m109uk/bioshere_logo_0000.bmp)

and its animated [avi (http://www.freepgs.com/m109uk/preview.avi)]

ps.
I have no artistic talent what so ever lol am still messing about so dont expect too much lol

Robert Kosek
18-03-2005, 02:16 AM
I like it. The "software" part is a little difficult to read, mainly the "w", due to it's size.


My site is: http://www.thewickedflea.com/ - Your source for everything Flea bitten. (every one's welcome, I am The Wicked Flea there ;))

The front page is just a placeholder at the moment, as I spent the greater half of my free time setting up the forum. The forum is anything but inactive right now... more people will sign up tomorrow, as half the previous members aren't in yet. From programming to tools and games, it's all there.

I'll be working on the front page tomorrow...

M109uk
19-03-2005, 03:19 AM
I like it. The "software" part is a little difficult to read, mainly the "w", due to it's size.
Cool, thanx..
Yeah its not so bad with the full sized image, but i was thinking about either using a different font or maybe rotating the viewport a bit more..

I want add more effects to it, see what happens lol


Your site looks great, look forward to seeing your finished frontpage ;)
I'l be signing up soon :D

Robert Kosek
21-03-2005, 11:50 PM
The front page is up and skinned, though Etomite is giving me some ridiculous issues ... and I'm considering Drupal. I also set up a blog, see the first link in my sig. I'll hold you to the signing up part. :D

Now I can get into programming some more Delphi stuff, I'll be using Asphyre... ;)

M109uk
22-03-2005, 06:53 PM
Looking good :D
Iv never used them personally, most of the time i wrote my own basi news mangement, how ever due to problems with my last server i converted to free management, but saves to a file on the server instead of a database :(

I'l probably convert this to a DB instead when i have time...

lol just signed up as M109uk there too :D

Cool, what kinda project if you dont mind me asking?

Robert Kosek
22-03-2005, 07:02 PM
Probably just a space based shooter, as simple as I can make it too. I'm still quite green when it comes to game programming.

I tend to use premade CMS systems as I'm not very good with PHP. I've read a bit and experimented though. ;) Your site is really good, even for a "basic news management". :D

If you're interested, check out [url]http://opensourcecms.com/[url] for a lot of PHP CMS and other scripts. It's a very good site.

I noticed your sign up, and am in the process of sending you a real PM welcome. ;)

M109uk
22-03-2005, 11:43 PM
Probably just a space based shooter, as simple as I can make it too. I'm still quite green when it comes to game programming.
Cool, is a great place to start :D Im guessing it'l be 2D..

haha im still a newbie too but as with all my projects i put myself in the deep end :roll:
XShell is my first game based program, most of it uses Win32 controls at the moment until i have the time to write a COMPLETE UI for XShell lol

I some times use premade scripts, either if i dont have the time to write my own code or for learning purposes.. I learnt most of what i know in my free time downloading free scripts from PHPBuilder (http://www.phpbuilder.com).. They have PHP scripts for almost everything :)

Thanx, i had to make a few mods to it to get it working the way it does, and i got the CMS control panel linked to logged in users of the website :D
Im still working on a few scripts (e.g. my screenshots, downloads and todo scripts, which are all file based too :()

Thanks, i will check it out later when i have time..

lol Yep i have just checked my email, and saw the message :) lol i'l post a message when i have made better progress with XShell ;)