Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Question about std::list and tidying up

This thread is locked; no one can reply to it. rss feed Print
Question about std::list and tidying up
DanielH
Member #934
January 2001
avatar

I was using my own list class and decided to switch it to std::list.

Two classes use it.

UndoList:
std::list<UndoData*> stack;
ThemeList:
std::list<ThemeInfo*> stack;

For some reason I am getting a break at the tidying up of ThemeList.

Quote:

Unhandled exception at 0x0041e062 in sudoku.exe: 0xC0000005: Access violation reading location 0x00000001.

sudoku.exe!std::list<ThemeInfo *,std::allocator<ThemeInfo *> >::clear() Line 612 + 0xf C++
sudoku.exe!std::list<ThemeInfo *,std::allocator<ThemeInfo *> >::_Tidy() Line 931 C++
sudoku.exe!std::list<ThemeInfo *,std::allocator<ThemeInfo *> >::~list<ThemeInfo *,std::allocator<ThemeInfo *> >() Line 366 C++
sudoku.exe!ThemeList::~ThemeList() Line 30 + 0xf C++

// in game.cpp
ThemeList       themeList;

// in themelist.cpp
ThemeList::ThemeList()
{
    this->screenShot = NULL;
    this->count = 0;
    this->place = 0;
    this->current = 0;
    this->over = -1;
}

ThemeList::~ThemeList()
{
} // line 30

This happens when even when I don't use themeList in the game. themeList only gets used when I am changing themes.

[EDIT]
I changed them to this and still get the same break.
std::list<UndoData> stack;
std::list<ThemeInfo> stack;

What could this be? Im using MSVC7.

CGamesPlay
Member #2,559
July 2002
avatar

You are deleteing a pointer that points to 0x00000001, which is invalid...

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ImLeftFooted
Member #3,935
October 2003
avatar

Set breakpoints at all your ctors and dtors. Namely ThemeList. See how many times the dtor is called and from where. Does the dtor get called more often then the ctor?

Other then that advice, theres not much I can figure out about what you're doing. Other then making some type of sudoku game. Try pasting your ThemeList and making the simpliest test case possible that shows the problem you're having (and obviously paste that too).

ReyBrujo
Moderator
January 2001
avatar

You need to use std::list<ThemeInfo*> stack;. Can you post the code of the constructor and destructor, as well as members of ThemeInfo?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

CGamesPlay
Member #2,559
July 2002
avatar

Okay, the first sentence in Rey's post isn't true. You can use lists of objects just fine.
[append]
To clarify, std::list<ThemeInfo> stack; is perfectly valid.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

DanielH
Member #934
January 2001
avatar

I placed breakpoints in ctor, dtor, and each member. ctor was getting called in the beginning of the program as ThemeList is a global. dtor was getting called at the end of the program. No members were getting called and I was still getting the same break.

I removed the breakpoints ant removed stack as a member of ThemeList and added it at the top of ThemeList's source. I am having a hard time trying to duplicate the same response. I am getting no break's.

Here is the header and source, the commented out parts are what it was.

1#ifndef _THEMELIST_H
2#define _THEMELIST_H
3 
4class ThemeList
5{
6public:
7 ThemeList();
8 ~ThemeList();
9 
10 static int addToList( const char *filename, int attrib, void *param );
11 void init();
12 void uninit();
13 void draw( BITMAP *bitmap );
14 void logic();
15 void moveUp();
16 void moveDown();
17 void apply();
18 void changeScreenShot();
19 char *getCurrentFilename();
20 
21private:
22 //std::list<ThemeInfo> themeStack;
23 RectInfo rectInfo[ 10 ];
24 BITMAP *screenShot;
25 int current;
26 int over;
27 int count;
28 int place;
29};
30 
31extern ThemeList themeList;
32 
33#endif

1#pragma warning( disable: 4312 )
2 
3#include <allegro.h>
4#include <time.h>
5#include <stdio.h>
6#include <string>
7#include <list>
8#include "defines.h"
9#include "globals.h"
10#include "module.h"
11#include "themefunctions.h"
12#include "theme.h"
13#include "themelist.h"
14 
15std::list<ThemeInfo> themeStack;
16 
17ThemeList::ThemeList()
18{
19 this->screenShot = NULL;
20 this->count = 0;
21 this->place = 0;
22 this->current = 0;
23 this->over = -1;
24}
25 
26ThemeList::~ThemeList()
27{
28}
29 
30 
31int ThemeList::addToList( const char *filename, int attrib, void *param )
32{
33 Theme temp;
34 char *extension = NULL;
35 
36 //std::list<ThemeInfo> *themeStack = (std::list<ThemeInfo>*)param;
37 
38 filename = get_filename( filename );
39 extension = get_extension( filename );
40 
41 *( extension - 1 ) = '\0';
42 
43 if ( temp.load( filename ) == 0 )
44 {
45 ThemeInfo theme;
46 
47 strcpy( theme.filename, filename );
48 strcpy( theme.themeName, temp.getName() );
49 
50 //themeStack->push_back( theme );
51 themeStack.push_back( theme );
52 
53 temp.unload();
54 }
55 
56 return 0;
57}
58 
59void ThemeList::init()
60{
61 char fileExt[ 128 ] = emptyString;
62 
63 sprintf( fileExt, themeText, "*", dllExtension );
64 
65 this->uninit();
66 
67 for_each_file_ex( fileExt,
68 FA_ARCH,
69 0,
70 ThemeList::addToList,
71 NULL );
72 //(void*)(&this->stack) );
73 
74 this->count = (int)themeStack.size();
75 
76 if ( this->count > 0 )
77 {
78 this->over = 0;
79 this->changeScreenShot();
80 this->over = -1;
81 
82 for ( int i = 0; i < 10; i++ )
83 {
84 theme.getThemeRectInfo( this->rectInfo[ i ], i );
85 }
86 }
87}
88 
89void ThemeList::uninit()
90{
91 if ( this->screenShot )
92 {
93 destroy_bitmap( this->screenShot );
94 this->screenShot = NULL;
95 }
96 
97 if ( themeStack.size() > 0 )
98 {
99 themeStack.clear();
100 }
101 
102 this->count = 0;
103 this->place = 0;
104 this->current = 0;
105 this->over = -1;
106}
107 
108void ThemeList::draw( BITMAP *bitmap )
109{
110 theme.drawThemeChoose( bitmap, this->screenShot );
111 
112 if ( themeStack.size() == 0 )
113 {
114 return;
115 }
116 
117 std::list<ThemeInfo>::iterator it = themeStack.begin();
118 for ( int i = 0; i < this->place; i++ )
119 {
120 it++;
121 }
122 
123 for ( int i = 0; i < 10; it++, i++ )
124 {
125 if ( it == themeStack.end() )
126 {
127 break;
128 }
129 
130 int index = i;
131 
132 if ( this->over == ( i + this->place ) )
133 {
134 index |= TF_OVER;
135 }
136 
137 if ( this->current == ( i + this->place ) )
138 {
139 index |= TF_CURRENT;
140 }
141 
142 if ( ( index & TF_OVER ) && ( index & TF_CURRENT ) )
143 {
144 index |= TF_BOTH;
145 }
146 
147 theme.drawThemeInfo( bitmap,
148 (*it),
149 index );
150 }
151}
152 
153void ThemeList::logic()
154{
155 if ( themeStack.size() == 0 )
156 {
157 return;
158 }
159 
160 std::list<ThemeInfo>::iterator it = themeStack.begin();
161 for ( int i = 0; i < this->place; i++ )
162 {
163 it++;
164 }
165 
166 this->over = -1;
167 
168 for ( int i = 0; i < 10; it++, i++ )
169 {
170 if ( it == themeStack.end() )
171 {
172 break;
173 }
174 
175 if ( mouseX >= rectInfo[ i ].x &&
176 mouseY >= rectInfo[ i ].y &&
177 mouseX < ( rectInfo[ i ].x + rectInfo[ i ].width ) &&
178 mouseY < ( rectInfo[ i ].y + rectInfo[ i ].height ) )
179 {
180 mousePtr = MP_HAND;
181 
182 this->over = ( i + this->place );
183 
184 if ( mouse1Clicked )
185 {
186 mouse1Clicked = false;
187 mousePtr = MP_ARROW;
188 
189 if ( this->current != this->over )
190 {
191 changeScreenShot();
192 }
193 }
194 }
195 }
196}
197 
198void ThemeList::changeScreenShot()
199{
200 Theme tempTheme;
201 
202 if ( this->screenShot )
203 {
204 destroy_bitmap( this->screenShot );
205 this->screenShot = NULL;
206 }
207 
208 this->current = this->over;
209 
210 if ( this->getCurrentFilename() )
211 {
212 if ( tempTheme.load( this->getCurrentFilename() ) == 0 )
213 {
214 this->screenShot = tempTheme.getScreenshot();
215 tempTheme.unload();
216 }
217 }
218}
219 
220void ThemeList::moveUp()
221{
222 if ( this->place > 0 )
223 {
224 this->place--;
225 }
226}
227 
228void ThemeList::moveDown()
229{
230 if ( this->place < ( this->count - 10 ) )
231 {
232 this->place++;
233 }
234}
235 
236char *ThemeList::getCurrentFilename()
237{
238 if ( themeStack.size() == 0 )
239 {
240 return NULL;
241 }
242 
243 std::list<ThemeInfo>::iterator it = themeStack.begin();
244 for ( int i = 0; i < this->current; i++ )
245 {
246 it++;
247 }
248 
249 return (*it).filename;
250}
251 
252void ThemeList::apply()
253{
254 strcpy( themeName, currentThemeName );
255}

ImLeftFooted
Member #3,935
October 2003
avatar

With this code

    for ( int i = 0; i < 10; it++, i++ )
    {
        if ( it == themeStack.end() )
        {
            break;
        }

You did good checking if it is invalid, but with two other loops you forgot to :x.

    std::list<ThemeInfo>::iterator it = themeStack.begin();
    for ( int i = 0; i < this->place; i++ )
    {
        it++;
    }

Never assume an iterator can be incremented and still valid

DanielH
Member #934
January 2001
avatar

this->place is always <= to themeStack.size();

Go to: