Jump to content


losing texture memory, using stl?


3 replies to this topic

#1 Stellar Developer

    New Member

  • Members
  • Pip
  • 5 posts

Posted 26 September 2005 - 03:38 PM

Intro :
I am attemping to write scene manager. I just need to display 1 or more 3d objects, loaded from .x files.

The Problem :
Renderable3dObject.InitGeometry(..) will load a xfile, material, and texture into its class. This works without any problems. If I take the class instance, and insert it into an stl (Standard template library) list. The texture memory becomes invalid. I dont understand why?

To Assist :
Here is the code im working with... Its extremely simplified and anyone with experience should be able to help. (*link here) Download the Source (Visual C++ 6 or 2003)

I would greatly appreciate any assistance :)

any ideas???
-sd
__________________________________________________


heres the basic outline...

initialize windows
initialize directx

// my problem is right below this, I can load the xfile and textures
// but once i leave the function, only the texture / material
// memory becomes invalid???
load world entities using a list ( ie.  stl::list<Renderable3dObject>  )
 L list<Renderable3dObject>.insert( myXFileClass )

main game loop
 L for all entities in list
    L e.RenderSelf( )   ( this does not work, because of invalid memory )

cleanup



#2 Stellar Developer

    New Member

  • Members
  • Pip
  • 5 posts

Posted 26 September 2005 - 09:48 PM

Thx for looking, the problem has been solved...

http://www.gamedev.n...topic_id=347841

#3 bladder

    DevMaster Staff

  • Moderators
  • 1057 posts

Posted 27 September 2005 - 03:49 AM

Since you had the problem solved, you should know why it was happening so as not to do it again. You were creating an object (Renderable3dObject) on the stack. Objects created on the stack are automatically taken care of for you. When you declare it, the constructor will be called, and when variable exits it's scope, the destructor will be called:

{
  SomeObject var; // contructor called

  // code
  // more code...

} // destructor of var called - var's scope finished.

So in your LoadEntities function you create a Renderable3dObject. But when that function exits, the destructor of that object is called. So the object becomes invalid. It only has a life time of the function it is created in. The way to solve this is by controlling an objects lifetime yourself. (which is what you are doing with new/delete and pointers)

#4 .oisyn

    DevMaster Staff

  • Moderators
  • 1810 posts

Posted 27 September 2005 - 08:45 AM

Also, could you please copy/paste the relevant pieces of code in your post? That way, we don't have to download zipfiles and browse through all the sourcefiles, which a lot of visitors simply don't want to do, although they could potentially solve your problem. Another drawback of posting links to code is that in a few months from now you'll have deleted your zipfile, so this topic becomes totally useless for future reference.

So, please, to help yourself and others: post relevant pieces of code on the forum, never link to it :)
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users