This site is no longer active and is available for archival purposes only. Registration and login is disabled.

Ownership surfaces


Ownership surfaces

Postby Phantom » Jul 2, 2002 @ 2:46pm

Give me some good data and
I will give you the world
User avatar
Phantom
pm Insider
 
Posts: 913
Joined: Feb 21, 2001 @ 8:14am
Location: Houten, Netherlands


On surface ownership policies

Postby s9801758 » Jul 2, 2002 @ 3:06pm

User avatar
s9801758
pm Member
 
Posts: 48
Joined: May 10, 2002 @ 10:20am
Location: Vancouver BC, Canada


Smart pointers hmmmm

Postby Rhino123 » Jul 2, 2002 @ 3:12pm

User avatar
Rhino123
pm Member
 
Posts: 66
Joined: Jul 2, 2002 @ 2:58pm
Location: Amsterdam


Postby Phantom » Jul 2, 2002 @ 3:18pm

Give me some good data and
I will give you the world
User avatar
Phantom
pm Insider
 
Posts: 913
Joined: Feb 21, 2001 @ 8:14am
Location: Houten, Netherlands


Postby s9801758 » Jul 2, 2002 @ 3:37pm

User avatar
s9801758
pm Member
 
Posts: 48
Joined: May 10, 2002 @ 10:20am
Location: Vancouver BC, Canada


Postby Rhino123 » Jul 2, 2002 @ 3:41pm

User avatar
Rhino123
pm Member
 
Posts: 66
Joined: Jul 2, 2002 @ 2:58pm
Location: Amsterdam


Postby s9801758 » Jul 2, 2002 @ 3:42pm

User avatar
s9801758
pm Member
 
Posts: 48
Joined: May 10, 2002 @ 10:20am
Location: Vancouver BC, Canada


Postby Phantom » Jul 2, 2002 @ 3:44pm

Give me some good data and
I will give you the world
User avatar
Phantom
pm Insider
 
Posts: 913
Joined: Feb 21, 2001 @ 8:14am
Location: Houten, Netherlands


Postby s9801758 » Jul 2, 2002 @ 3:49pm

"If you create a surface, the ref count is zero. No one uses it."

Theoritically this is incorrect. First of all, objects with a zero-refcount have no right to exist. When you create an object its refcount is one, because the place where you created it has a reference (until it goes out of scope, that is).

"Then if that object is deleted, it will decrease the ref count, and delete the object."

I assume that what you mean is that the sprite will decrease the surface's refcount, and when the surface's refcount equals zero the surface deletes itself.

However, imagine the following case:

Suppose you have a character able to shoot bullets. Obviously all bullets can share the same surface. However, where does the surface exist when no bullets are fired? Since no bullet-sprites exist, no references to the surface exist, and hence the surface will delete itself. This is undesired because you don't want to reload the surface when you shoot a new bullet.

Hence, you have to maintain a persistent reference. The only way you can do this is by a manual AddRef in your client-application.

Hence the client code WILL need to do his own AddRef and Release in some places.

Good hand-written refcounting IS a HARD problem and difficult to maintain. At first reference-counting seems an easy solution to memory-leaks, but in practice it doesn't work that way. It is too easy to lose a reference without releasing it, especially without scoped_references (yuck, another scoped class, please...)

Cheers,

Jape
User avatar
s9801758
pm Member
 
Posts: 48
Joined: May 10, 2002 @ 10:20am
Location: Vancouver BC, Canada


Consistency

Postby Rhino123 » Jul 2, 2002 @ 3:49pm

If you want consistency I would go with option 1: sprites are never the owner unless specifically mentioned with the 'setowner' command.

this way it is easy to remember: anything you NEW, you must DELETE

R

p.s. We can than add a very easy debugging memoryleak detector where the filename and linenumber of any new'ed surface or sprite is stored and is later displayed when exitting the library.
User avatar
Rhino123
pm Member
 
Posts: 66
Joined: Jul 2, 2002 @ 2:58pm
Location: Amsterdam


Postby s9801758 » Jul 2, 2002 @ 3:53pm

Heck, let's take it even further and explicitly dissallow giving ownership of the surface to the sprite! How often are we going to use it anyway? It's not too difficult to maintain both the surface and sprite resource in one place, and sharing is too common anyway!

Thus, remove the sprite->SetFlag( OWNER ) option.

This also agrees with the common C++ RAII (resource aquisition is initialization) idiom.

Mmmm, thinking about this, I think this is indeed the best solution. It is easy, clean and forces the user to think about surface-lifetimes.

About the memory-manager. I have one lying here from a previous hobby-project. It doesn't use the STL (try writing one which uses the STL. It's nearly impossible since the STL uses its own allocators in combination with (placement) new and delete), and should be very easy to fit in. Because if overrrides global new and delete it can be used immmediately.

Cheers,

Jape
User avatar
s9801758
pm Member
 
Posts: 48
Joined: May 10, 2002 @ 10:20am
Location: Vancouver BC, Canada


memory allocater?

Postby Rhino123 » Jul 2, 2002 @ 4:09pm

Why would you want a memory allocater?

if it is for debugging alone, you can also add some debugging funtions without adding a full fledged memory allocater now couldn't you?

I think (hope) windows can do memory allocations much better then we can...

R
User avatar
Rhino123
pm Member
 
Posts: 66
Joined: Jul 2, 2002 @ 2:58pm
Location: Amsterdam


Postby s9801758 » Jul 2, 2002 @ 4:14pm

Whoops, slight misunderstanding here. My bad.

I meant a simple memory-tracker. Something that just overloads global new and delete and gets the line and file as two extra (implicit, hidden through a macro) parameters. These are just passed on to malloc and free after being put in a list to track them...

Cheers,

Jape
User avatar
s9801758
pm Member
 
Posts: 48
Joined: May 10, 2002 @ 10:20am
Location: Vancouver BC, Canada


memory trackers

Postby Rhino123 » Jul 2, 2002 @ 4:19pm

I don't think that would help a lot since this is already present in VisualC if you run it in debug mode.

The preoblem with your suggested scheme is that the surface an sprite class are NEW'ed in the DLL, everytime on the exact same place. It would thus be more convenient to know which source called the appropriate factory method

R
User avatar
Rhino123
pm Member
 
Posts: 66
Joined: Jul 2, 2002 @ 2:58pm
Location: Amsterdam


Postby BurningSheep » Jul 2, 2002 @ 4:27pm

Don't mean to interrupt the conversation but...
you guys are like sitting in the same office, right? :lol:

neway, please continue ;)
Do you want custom Snails levels? Click
User avatar
BurningSheep
pm Insider
 
Posts: 1226
Joined: Apr 12, 2002 @ 11:49pm
Location: The Netherlands


Next

Return to Phantom's Forum


Sort


Forum Description

Discuss any of Phantom's projects here (Operation Nutcracker, etc.)

Moderators:

sponge, RICoder, Phantom

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum