Archived

This topic is now archived and is closed to further replies.

Stl question

This topic is 5668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been using stl (and boy is it a time saver!) to handle scene management in my game engine. Well i've decided to add a texture manager for the engine using the map stl. i'm mapping using a char* (the name of the file) and a LPDIRECT3DTEXTURE8 like so:
          
map<char*, LPDIRECT3DTEXTURE8>				Textures;
  
well i've got it working all nice and fine execpt when I insert things. Like so:
  
typedef pair<char*, LPDIRECT3DTEXTURE8>	  StrTexPair;
Textures.insert(StrTexPair(filename, temp));
//temp is a LPDIRECT3DTEXTURE8 that's been properly created  
It crashes with a memory read violation. I've spent a good time stepping through line by line (even so in the stl code). I've narrowed it down to where it crashes in the stl code when it tries to retrieve the root node and assign it to a temp value (for it's self). The root() function calls the parent() in the stl and they seem to return NULL's just fine (considering the list is empty when it crashes). i've also changed my map to a list,vector, and queue. they all crash when trying to do the same thing. So my next thought was that LPDIRECT3DTEXTURE8 had something to do with it. i'm i correct in using LPDIRECT3DTEXTURE8? i've even tried using LPDIRECT3DTEXTURE8*, which doesn't work either. So i'm not sure what's up with the memory management on this thing? Anybody got any ideas? -Pac EDIT: boy those source tags can be annoying [edited by - Pactuul on June 3, 2002 2:58:19 PM] [edited by - Pactuul on June 3, 2002 2:59:05 PM]

Share this post


Link to post
Share on other sites
Don't use char *s, go with std::string. You're probably storing pointers to strings that get deleted, and are referencing nonexistent data.

Also, check out std::pair and std::make_pair.


[edited by - IndirectX on June 3, 2002 3:09:06 PM]

Share this post


Link to post
Share on other sites
I was actually trying to use strings, but I forgot to do #include <string> so it was using another form of string. but i fixed that and am using strings.

Still having that problem. I did try changing LPDIRECT3DTEXTURE8 to an int and it works now, so i''m thinking LPDIRECT3DTEXTURE8 is causing the problem. Is it because it''s a pointer?

Share this post


Link to post
Share on other sites
No, STL maps can carry pointers fine, and it doesn''t do anything to/with the second type in the map--that''s just dumb data that sits there. Are you sure your textures are valid when you put them in the map? You aren''t releasing them or anything after putting them in the map? It''d help if you show the code where you use the texture that generates an error, e.g. exactly how you get the texture pointer from the map & how you use it. Also, you might want to try logging the texture pointer values when you put them into the map and when you take them out--they should be the same, and if they''re not, maybe you''re overwriting map entries or something. If they are the same, then it sounds like that pointer becomes invalid before you try using it.

Share this post


Link to post
Share on other sites
damn I feel stupid...that''s odd though, because I had to create a texture correctly or it would fail. Because the first time through the device wasn''t being created so it would create a texture, but I was still hoping it would insert into the list. Well thanks for the help guys!

Share this post


Link to post
Share on other sites
Just because I''m curious and once considered something similar, how''s the performance for your map doing? Do you access the map a lot each frame? I was always worried that the map access would be too slow to use frequently, but sometimes it''s much more convenient to reference something by a string-represented name than by a number (array/vector index). If it doesn''t significantly hurt performance, maybe I''ll start using maps more often.

Share this post


Link to post
Share on other sites
It runs pretty good.

my scene manage is a parent-child relationship. Each object contains a list (stl of course) of all it''s children. I was trying to write my own attaching and detaching and memory handling and deriving objects from them. it was a headache. then I realize I could use the stl lists. In like 1 hr I rewrote the code using it. It actually was faster (probably since mine was not optimized at all), fully functional (I could access and sort things easier) and less code to debug. Also I didn''t have to do type casting as much.

Well so now i''m using maps to handle my textures and my meshs. It was a big boost, since before I was loading in lots of textures and meshes, but most of them are duplicates, so memory usage and loading speeds were horrible. Using maps, it was a lot faster, and it seems pretty efficent too. I like the fact that if you map something with a key that''s already mapped it won''t re-create another texture or mesh.

I''ve been using strings to map the textures and meshs, and it''s a lot easier and cleaner. In terms of speed alone, I think it holds up pretty well.


-Pac

Share this post


Link to post
Share on other sites
I think the point IndirectX was making is that it''s a bad idea to use char* as the key type in the map, since this means comparisons are between addresses rather than strings, so a copy of a key, stored in a different character buffer, would not access the correct data.
Anyway, if the guy''s changed to using std::string this can''t be the problem.

Cameron

Share this post


Link to post
Share on other sites
[tangential but useful:]
While on the topic of simplifying object management (using maps, etc), you might also want to consider using CComPtr to manage all your COM interfaces (DirectX). You''ll never have to call Release or write another SAFE_DELETE macro again. It''s in the <altbase.h> header file, and is a real time saver. You can stick references to them in your map if space is an issue.

Share this post


Link to post
Share on other sites
Oluseyi- while looking for solutions and such in the forums, i''ve seen you talk about the CComPtr(). i''ve read up on it, but i''m not sure how to apply it correctly in my case. In other words, how would you implement into the code correctly?

Share this post


Link to post
Share on other sites
quote:
Original post by Pactuul
Oluseyi- while looking for solutions and such in the forums, i''ve seen you talk about the CComPtr(). i''ve read up on it, but i''m not sure how to apply it correctly in my case. In other words, how would you implement into the code correctly?

In general, you would use CComPtr like so:

  
CComPtr <IInterface> interface;
CoCreateInstance(..., &interface);
// Don''t call Release; CComPtr will call it for you when interface goes out of scope


Keep in mind that you can''t generally use CComPtr in containers because it overloads operator &. I had a big problem with std::vector''s of CComPtr''s of IDirect3DTexture8''s due to this; check my profile for the thread link. There are some code snippets in that thread as well.

Share this post


Link to post
Share on other sites