C++编程思想第一卷第16章P401到P405的一个很奇怪的问题(附有完整代码)
详细请看我上传的附件。(使用vs2005编译环境)用下面的代码建个工程也行。
现在问题是这样的:
在main()函数中,如果没有把“cout << acStash.remove(0) << endl;” 注释掉,~CleanupCheck()会被调用了很多次。但在所有代码中只有一个静态的CleanupCheck对象,程序结束时,其析构函数应该只被掉用一次才对啊(大多数教材是这样说的),但事实上被调用了很多次,用单步调试,还把栈给冲垮了。我猜这应该是运行时库函数的机制吧。期待高手指点......
//TPStash.h
#ifndef TPSTASH_H
#define TPSTASH_H
#include "../../require.h"
template<class T, int incr = 10>
class PStash
{
int quantity;
int next;
T** storage;
void inflate( int increase = incr );
public:
PStash() : quantity(0), next(0), storage(0) {}
~PStash();
int add( T* element );
T* operator[] ( int index ) const;
T* remove( int index );
int count() const { return next; }
};
template<class T, int incr>
int PStash<T, incr>::add( T* element )
{
if ( next >= quantity )
inflate( incr );
storage[next++] = element;
return ( next - 1 );
}
template<class T, int incr>
PStash<T, incr>::~PStash()
{
for ( int i = 0; i < next; i++ )
{
delete storage[i];
storage[i] = 0;
}
delete []storage;
}
template<class T, int incr>
T* PStash<T, incr>::operator [] ( int index ) const
{
require( index >= 0, "PStash::operator[] index negative" );
if ( index >= next )
return 0;
require( storage[index] != 0, "PStash::operator[] returned null pointer" );
return storage[index];
}
template<class T, int incr>
T* PStash<T, incr>::remove ( int index )
{
T* v = operator[] ( index );
if ( v != 0 ) storage[index] = 0;
return v;
}
template<class T, int incr>
void PStash<T, incr>::inflate ( int increase )
{
const int psz = sizeof ( T* );
T** st = new T*[quantity + increase ];
memset( st, 0, ( quantity + increase ) * psz );
memcpy( st, storage, quantity * psz );
quantity += increase;
storage = st;
}
#endif
//AutoCounter.h
#ifndef AUTOCOUNTER_H
#define AUTOCOUNTER_H
#include "../../require.h"
#include <iostream>
#include <set>
#include <string>
class AutoCounter
{
static int count;
int id;
class CleanupCheck
{
std::set< AutoCounter*> trace;
public:
void add ( AutoCounter* ap )
{
trace.insert( ap );
}
void remove ( AutoCounter* ap )
{
require( trace.erase( ap ) == 1, "Attempt to delete AutoCounter twice" );
}
~CleanupCheck()
{
std::cout << "~CleanupCheck()" << std::endl;
require(trace.size() == 0,
"All AutoCounter objects not cleaned up" );
}
};
static CleanupCheck verifier;
AutoCounter() : id ( count++ )
{
verifier.add ( this );
std::cout << "created[" << id << "]" << std::endl;
}
AutoCounter( const AutoCounter& );
void operator= ( const AutoCounter& );
public:
static AutoCounter* create()
{
return new AutoCounter();
}
~AutoCounter()
{
std::cout << "destroying[" << id << "]" << std::endl;
verifier.remove( this );
}
friend std::ostream& operator<<( std::ostream& os, const AutoCounter& ac )
{
return os << "AutoCounter " << ac.id;
}
friend std::ostream& operator <<( std::ostream& os, const AutoCounter* ac )
{
return os << "AutoCounter " << ac->id;
}
};
#endif
//AutoCounter.cpp
#include "AutoCounter.h"
AutoCounter::CleanupCheck AutoCounter::verifier;
int AutoCounter::count = 0;
//TPStashTest.cpp
#include "AutoCounter.h"
#include "TPStash.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
PStash<AutoCounter, 1> acStash;
acStash.add(AutoCounter::create() );
//把下面的注释去掉后,~CleanupCheck()被调用了很多次
//cout << acStash.remove(0) << endl;
return 0;
}
//require.h
//: :require.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Test for error conditions in programs
// Local "using namespace std" for old compilers
#ifndef REQUIRE_H
#define REQUIRE_H
#include <cstdio>
#include <cstdlib>
#include <fstream>
inline void require(bool requirement,
const char* msg = "Requirement failed") {
using namespace std;
if (!requirement) {
fprintf(stderr, "%s", msg);
exit(1);
}
}
inline void requireArgs(int argc, int args,
const char* msg = "Must use %d arguments") {
using namespace std;
if (argc != args + 1) {
fprintf(stderr, msg, args);
exit(1);
}
}
inline void requireMinArgs(int argc, int minArgs,
const char* msg =
"Must use at least %d arguments") {
using namespace std;
if(argc < minArgs + 1) {
fprintf(stderr, msg, minArgs);
exit(1);
}
}
inline void assure(std::ifstream& in,
const char* filename = "") {
using namespace std;
if(!in) {
fprintf(stderr,
"Could not open file %s", filename);
exit(1);
}
}
inline void assure(std::ofstream& in,
const char* filename = "") {
using namespace std;
if(!in) {
fprintf(stderr,
"Could not open file %s", filename);
exit(1);
}
}
#endif // REQUIRE_H ///:~