Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Segmentation Error
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Bored




PostPosted: Fri Jan 27, 2006 3:37 pm   Post subject: Segmentation Error

OK, so I'm creating a base for a turing RPG with a cool fighting system I conjured up one day. So as of now I have a simple object oriented base to work off of and I have collision movement and such. The way it works is that by clicking with the left click you sprint to where you clicked, middle clicking allows you to walk to where you where you clicked, and right clicking turns you in the direction of clicking without changing where your moving. If you right click while sprinting you change speed to walking speed. You can also target enemies by clicking on them. When an enemy is targeted you can sprint towards them. you can also move while still facing them using the middle mouse button. Finally this is where the probelm happens. If you press one you shoot arrows and every now and then when your player hits an arrow i get a segmantaion error and I don't know why. I'm not completely sure what a segmentation error is but I know it has to do with accesing memory. Oh ya before I finish if you don't have a three button mouse don't fret because Enter does the same as the middle mouse button and Ctrl the same as the right mouse button.


RPG Test.t
 Description:
Source Code

Download
 Filename:  RPG Test.t
 Filesize:  12.96 KB
 Downloaded:  145 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Fri Jan 27, 2006 4:45 pm   Post subject: (No subject)

What exactly is the segmentation error?

You say "every now and then". Does that mean it's possible that you could get this error on the first shot? The second? Does it only occur after shooting a bunch of arrows? If so, you need to free your objects.
Bored




PostPosted: Mon Jan 30, 2006 2:05 pm   Post subject: (No subject)

OMG I can't beleive i forgot to free Objects when they we're destroyed. But turns out that is not the problem. The problem seems to be happening randomly when the object is shot and happens at line 126. I tried testing to see if it happened when COLLIDED (O (t)) was called or when O (t) ~= self was called and it happens at either situation. It seems that for some reason I get an error when trying to acces certain arrows. Hmmmmmmmmmmm......
Bored




PostPosted: Mon Sep 04, 2006 8:56 pm   Post subject: (No subject)

Well I was looking at this again today after looking through old programs and noticed that I never did fix the problem. I did a little testing and noticed that when the error happens at line 126
code:
if COLLIDED (O (t)) and O (t) ~= self then %if pawn hit object and object isnt pawn

every time t = upper (O). I still have no idea why this is happening but I thought maybe this might help a bit.
[/code]
ericfourfour




PostPosted: Tue Sep 05, 2006 2:28 pm   Post subject: (No subject)

I also noticed in the same procedure you use trig functions over and over. These will slow down the program a lot. That can be optimized by simply memorizing there value.

About the error, it seems random and I can't seem to isolate it, let alone figure out what it is.
ericfourfour




PostPosted: Tue Sep 05, 2006 3:03 pm   Post subject: (No subject)

Okay I think your error is a memory leak beacuase you just lower the upper bound of the Objects array without keeping track of the object to which the last element was pointing.

code:

Line 397

Objects (i) := Objects (upper (Objects))
new Objects, upper (Objects) - 1

See, nothing to manage the object.


This can be fixed by either freeing the objects every time (which would be very inneficient), or you could keep track of the objects that aren't in use with a flag (such as "deleted") and just reuse them when needed.

Also, in the collision function you check with a bunch of sizes when the objects are merely circles and collision can be checked with the distance formula ((x2 - x1)^2 + (y2 - y1)^2)^0.5. Well, unless you are also checking collision with arrows.

Furthermore, at the end of the program, all of the object should be freed because if there was a successful execution and none of the objects were freed, the user would be stuck with all of the extra data on their memory.
Bored




PostPosted: Tue Sep 05, 2006 7:48 pm   Post subject: (No subject)

ericfourfour wrote:
Okay I think your error is a memory leak beacuase you just lower the upper bound of the Objects array without keeping track of the object to which the last element was pointing.

code:

Line 397

Objects (i) := Objects (upper (Objects))
new Objects, upper (Objects) - 1

See, nothing to manage the object.


This can be fixed by either freeing the objects every time (which would be very inneficient), or you could keep track of the objects that aren't in use with a flag (such as "deleted") and just reuse them when needed.


I actually did free the objects every time, I don't know why it isn't in the version I uploaded. So I'm still not sure where the error is lying.

ericfourfour wrote:
Also, in the collision function you check with a bunch of sizes when the objects are merely circles and collision can be checked with the distance formula ((x2 - x1)^2 + (y2 - y1)^2)^0.5. Well, unless you are also checking collision with arrows.


Well yes I'm drawing circles now but basically I was planning on having just simple rectangle collision, and all objects will be rectangles so that when I add in buildings and such things will be much simpler.

ericfourfour wrote:
Furthermore, at the end of the program, all of the object should be freed because if there was a successful execution and none of the objects were freed, the user would be stuck with all of the extra data on their memory.

Ok I'll do that. Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: