Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > MSVC 2003: ctor-try-block error: possible compiler bug

Reply
Thread Tools

MSVC 2003: ctor-try-block error: possible compiler bug

 
 
ksukhonosenko@yandex.ru
Guest
Posts: n/a
 
      09-26-2006
This message was originally posted to comp.lang.c++.moderated
----------------------------------------------------------------------------------------------

Hi!

I face a problem in my production code. I could deduce this problem to
program shown below. It seems, that try-block in constructor doesnt
work as it should (compared to case where no try block exists at all).
I tested this small program on my MSVC .NET Pro 2003 (and separately on
PC with MSVC2003 SP1 installed). In both cases I experienced the same
behaviour - access violation. Please see comment inside the program
body. To get this sample work - just comment out try block in ctor.

Have anyone faced such behaviour before? Is there known workarounds?
Thanks.
----
// ref_count.cpp : ctor try block invalid behaviour test

// if ctor uses try block (X::X), program crashes, trying to
dereferencing danglig pointer
// If try block is disabled, everything goes fine.

// I experienced this problem in my production code which used
boost::intrusive_ptr class.
// I wrote similar class (CountingPtr) for simplicty's sake (and to
kill dependencies) and results are the same - program crashes

// tested on compilers: MS VS.NET 2003 (both original & SP1)

#include <iostream>
#include <cassert>

using namespace std;

struct IRefCounted
{
virtual void addRef() = 0;
virtual void release() = 0;

virtual ~IRefCounted() {};
};

struct ILogger : public IRefCounted
{
void virtual write(const char * text) = 0;
};

class Logger : public ILogger
{
int m_ref;
~Logger(){}
public:
Logger() : m_ref(0)
{
}

void addRef()
{
m_ref++;
}

void release()
{
if(--m_ref == 0)
delete this;
}

void write(const char * text)
{
cout << text << endl;
}
};

template<typename T>
class CountingPtr
{
T * m_t;

public:
CountingPtr(T * t, bool addRef = true) : m_t(t)
{
assert(t!=0);
if(addRef)
t->addRef();
}

~CountingPtr()
{
if(m_t)
m_t->release();
}

CountingPtr&operator=(const CountingPtr & rhs)
{
if(this == &rhs)
assert(false);
else
{
m_t = rhs.m_t;
m_t->addRef();

}
return *this;
}

T * operator->()
{
return m_t;
}

CountingPtr(const CountingPtr & rhs)
{
*this = rhs;
}
};

typedef CountingPtr<ILogger> CLoggerPtr;


void use(CLoggerPtr logger)
{
logger->write("use");
}

class Loggable
{
public:
Loggable(CLoggerPtr logger) : m_logger(logger)
{}

private:
CLoggerPtr m_logger;
};

class X : public Loggable
{
public:
X(CLoggerPtr logger)
try : Loggable(logger) // comment try block out to
get working code!
{}
catch(std::runtime_error & )
{
throw;
};
};

int main(int argc, char* argv[])
{
try
{
CLoggerPtr logger(new Logger());
use(logger);

{
X y1(logger); // THIS line decrements ref counter by 2 in case try
block is active
}

{
X y2(logger); // OOPS IS HERE! If try block is in act, program tries
to reference dangling pointer
}

return 0;
}
catch(exception & ex)
{
cout << "Exception: " << ex.what() << endl;
return EXIT_FAILURE;
}
catch(...)
{
cout << "(...) exception" << endl;
return EXIT_FAILURE;
}
}

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> This message was originally posted to comp.lang.c++.moderated
> ----------------------------------------------------------------------------------------------
>
> Hi!
>
> I face a problem in my production code. I could deduce this problem to
> program shown below. It seems, that try-block in constructor doesnt
> work as it should (compared to case where no try block exists at all).
> I tested this small program on my MSVC .NET Pro 2003 (and separately on
> PC with MSVC2003 SP1 installed). In both cases I experienced the same
> behaviour - access violation. Please see comment inside the program
> body. To get this sample work - just comment out try block in ctor.
>
> Have anyone faced such behaviour before? Is there known workarounds?
> Thanks.
> ----
> // ref_count.cpp : ctor try block invalid behaviour test
>
> // if ctor uses try block (X::X), program crashes, trying to
> dereferencing danglig pointer
> // If try block is disabled, everything goes fine.
>
> // I experienced this problem in my production code which used
> boost::intrusive_ptr class.
> // I wrote similar class (CountingPtr) for simplicty's sake (and to
> kill dependencies) and results are the same - program crashes
>
> // tested on compilers: MS VS.NET 2003 (both original & SP1)
>
> #include <iostream>
> #include <cassert>
>
> using namespace std;
>
> struct IRefCounted
> {
> virtual void addRef() = 0;
> virtual void release() = 0;
>
> virtual ~IRefCounted() {};
> };
>
> struct ILogger : public IRefCounted
> {
> void virtual write(const char * text) = 0;
> };
>
> class Logger : public ILogger
> {
> int m_ref;
> ~Logger(){}
> public:
> Logger() : m_ref(0)
> {
> }
>
> void addRef()
> {
> m_ref++;
> }
>
> void release()
> {
> if(--m_ref == 0)
> delete this;
> }
>
> void write(const char * text)
> {
> cout << text << endl;
> }
> };
>
> template<typename T>
> class CountingPtr
> {
> T * m_t;
>
> public:
> CountingPtr(T * t, bool addRef = true) : m_t(t)
> {
> assert(t!=0);
> if(addRef)
> t->addRef();
> }
>
> ~CountingPtr()
> {
> if(m_t)
> m_t->release();
> }
>
> CountingPtr&operator=(const CountingPtr & rhs)
> {
> if(this == &rhs)
> assert(false);
> else
> {
> m_t = rhs.m_t;
> m_t->addRef();
>
> }
> return *this;
> }
>
> T * operator->()
> {
> return m_t;
> }
>
> CountingPtr(const CountingPtr & rhs)
> {
> *this = rhs;
> }
> };
>
> typedef CountingPtr<ILogger> CLoggerPtr;
>
>
> void use(CLoggerPtr logger)
> {
> logger->write("use");
> }
>
> class Loggable
> {
> public:
> Loggable(CLoggerPtr logger) : m_logger(logger)
> {}
>
> private:
> CLoggerPtr m_logger;
> };
>
> class X : public Loggable
> {
> public:
> X(CLoggerPtr logger)
> try : Loggable(logger) // comment try block out to
> get working code!
> {}
> catch(std::runtime_error & )
> {
> throw;
> };
> };
>
> int main(int argc, char* argv[])
> {
> try
> {
> CLoggerPtr logger(new Logger());
> use(logger);
>
> {
> X y1(logger); // THIS line decrements ref counter by 2 in case try
> block is active
> }
>
> {
> X y2(logger); // OOPS IS HERE! If try block is in act, program tries
> to reference dangling pointer
> }
>
> return 0;
> }
> catch(exception & ex)
> {
> cout << "Exception: " << ex.what() << endl;
> return EXIT_FAILURE;
> }
> catch(...)
> {
> cout << "(...) exception" << endl;
> return EXIT_FAILURE;
> }
> }


This code (with the try block) works fine with g++. It even passes a
valgrind check. Maybe, you are better of in a compiler specific news group.
Also, you might consider submitting a bug report to your compiler vendor.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
ksukhonosenko@yandex.ru
Guest
Posts: n/a
 
      09-27-2006
Thanks,

Could someone recommend me alive group related to MS VC++ compiler?

Thanks,
Kirill

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      09-27-2006
(E-Mail Removed) wrote:
> Thanks,
>
> Could someone recommend me alive group related to MS VC++ compiler?
>
> Thanks,
> Kirill
>


http://www.parashift.com/c++-faq-lit...t.html#faq-5.9
 
Reply With Quote
 
ksukhonosenko@yandex.ru
Guest
Posts: n/a
 
      09-27-2006
Hi!

This discusion is mode here -
http://groups.google.com/group/comp....b4bc995e3975e6

Regards,
Kirill

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
msvc compiler Rick C++ 2 12-04-2007 06:43 PM
MSVC++ 6.0 Linking problem (possible OT) hamishd C++ 3 08-31-2006 02:40 AM
MSVC++ 6.0 Complier optimization bug?? jryden C++ 11 04-28-2006 04:43 PM
Want workaround for msvc compiler (VC++ 6.0). Jason Heyes C++ 6 06-15-2005 05:39 PM
compiler version mismatch when building ext_modules with MSVC Bo Peng Python 0 01-09-2005 09:53 PM



Advertisments