From: Rob C. <rob...@at...> - 2002-01-31 00:48:27
|
I found out about this the hard way. You may already be aware of it. Is there a way to prevent it? Maybe by preventing assignment of normal pointers to smart pointers? In the following program, the assignment of either aPtr or the address of aSpatial (depending on the definition of INNER_CRASH) to aSPtr will ref() that object, increasing its reference count from 0 to 1. When aSPtr goes out of scope, its destructor will unref() the object, decreasing its reference count from 1 to 0. When this happends, unref() actually deletes the object. If INNER_CRASH is defined, this means it will try to delete aSpatial and crash immediately. If INNER_CRASH is not defined, it will delete the object pointed to by aPtr. The program will then crash as soon as soon as it tries to acces the object via "aPtr->valid_time(0)". Rob Campbell #include <iostream> #include <vxl/vxl/vcsl/vcsl_spatial.h> #define INNER_CRASH int main() { vcsl_spatial aSpatial; vcsl_spatial *aPtr = new vcsl_spatial; { #ifdef INNER_CRASH vcsl_spatial_sptr aSPtr = &aSpatial; #else vcsl_spatial_sptr aSPtr = aPtr; #endif } std::cout << "Outside of inner scope\n"; aPtr->valid_time(0); return 1; } Robert J. Campbell Jr. rob...@at... |