473,666 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Debug Assertion Failed" On Smart Pointer

I read a section of my text on smart "counting" pointers and found it
confusing so I decided to get hands-on.

however I'm getting "Debug Assertion Failed... Expression:
_BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)" whenever I run my code below.
program works fine after I bypass the msg.

any ideas? thx a lot.
#include "stdafx.h"

#using <mscorlib.dll >

using namespace std;

template <class T> class CountedPtr
{
public:

T* ptr;
long* count;

void dispose()
{
if((--(*count)) == 0)
{
delete count;
delete ptr;
}
}

explicit CountedPtr(T* p = 0) : ptr(p), count(new long(1))
{
}

CountedPtr(cons t CountedPtr <T>& p) throw() : ptr(p.ptr),
count(p.count)
{
++(*count);
}

~CountedPtr() throw()
{
dispose();
}

CountedPtr <T>& operator= (const CountedPtr <T>& p) throw()
{
if(this != &p)
{
dispose();
ptr = p.tr;
count = p.count;
++(*count);
}
return *this;
}

T& operator*() const throw()
{

return *ptr;
}

T* operator->() const throw()
{
return ptr;
}
};

int _tmain()
{
int i = 10;
int* sampleIntPtr = &i;

cout << *sampleIntPtr << "\n\n";

CountedPtr <int> countedIntPtr(s ampleIntPtr);
CountedPtr <int> countedIntPtr2 = countedIntPtr;

cout << *countedIntPtr << "\n\n";
cout << *countedIntPtr2 << "\n\n";
cout << *(countedIntPtr .count) << "\n\n";
cout << *(countedIntPtr 2.count) << "\n\n";

CountedPtr <int> countedIntPtr3 = countedIntPtr2;
CountedPtr <int> countedIntPtr4 = countedIntPtr2;

cout << *(countedIntPtr .count) << "\n\n";

return 0;
}

Oct 6 '05 #1
4 4297
* A_*********@hot mail.com:
I read a section of my text on smart "counting" pointers and found it
confusing so I decided to get hands-on.
You read what you had written yourself, and got confused?

however I'm getting "Debug Assertion Failed... Expression:
_BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)" whenever I run my code below.
program works fine after I bypass the msg.
Most probably not (works fine, that is).

any ideas? thx a lot.
#include "stdafx.h"
This is not a standard header.

#using <mscorlib.dll >
This is a non-standard directive, a language extension for some compiler.

using namespace std;

template <class T> class CountedPtr
{
public:

T* ptr;
long* count;
Should not be public.

void dispose()
{
if((--(*count)) == 0)
{
delete count;
delete ptr;
}
}


Already you're in deep waters. 'dispose' allows the client code to invalidate
the 'count' and 'ptr' members, and then call 'dispose' again, bang crash.

Also, this kind of logic belongs in the destructor.

Leveraging constructors and destructors is what smart-pointers are all about,
implementation-wise, so that's what you should do if you're implementing a
smart-pointer.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 6 '05 #2
Alf P. Steinbach wrote:
* A_*********@hot mail.com:
I read a section of my text on smart "counting" pointers and found it
confusing so I decided to get hands-on.


You read what you had written yourself, and got confused?

textbook.

#include "stdafx.h"


This is not a standard header.

#using <mscorlib.dll >


This is a non-standard directive, a language extension for some compiler.

using Visual Studio 2003 which automatically generated those.

using namespace std;

template <class T> class CountedPtr
{
public:

T* ptr;
long* count;


Should not be public.

okay!

void dispose()
{
if((--(*count)) == 0)
{
delete count;
delete ptr;
}
}


Already you're in deep waters. 'dispose' allows the client code to invalidate
the 'count' and 'ptr' members, and then call 'dispose' again, bang crash.

Also, this kind of logic belongs in the destructor.

Leveraging constructors and destructors is what smart-pointers are all about,
implementation-wise, so that's what you should do if you're implementing a
smart-pointer.

thx for shedding light on that.

I was just wondering if you could discuss the single argument
constructor:
explicit CountedPtr(T* p = 0) : ptr(p), count(new long(1))
{
}
namely, why is 'explicit' being used? why is the value zero being
assigned to the pointer argument? and what's with the ':'? it doesn't
look like inheritance has any relevance here.

thx again.

Oct 6 '05 #3
* A_*********@hot mail.com:

I was just wondering if you could discuss the single argument
constructor:

explicit CountedPtr(T* p = 0) : ptr(p), count(new long(1))
{
}
namely, why is 'explicit' being used?
It prevents automatic (implicit) conversion of a T* to a CountedPtr where a
CountedPtr is expected, but why the designed wanted that only s/he knows.

why is the value zero being assigned to the pointer argument?
That's a default value.

and what's with the ':'? it doesn't
look like inheritance has any relevance here.


That's a constructor initializer list; I think it's discussed in the FAQ.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 6 '05 #4
A_*********@hot mail.com wrote:
{
int i = 10;
int* sampleIntPtr = &i;

cout << *sampleIntPtr << "\n\n";

CountedPtr*<int >*countedIntPtr (sampleIntPtr);


Here you're binding a smart pointer to the address of a local automatic
variable. This makes no sense. Smart pointers serve the purpose of
automatically releasing dynamically allocated objects when they're no
longer needed. Your code, then, ends up with invoking 'delete' on a pointer
which hasn't been returned by 'new', which, of course, yields undefined
behaviour.

Max
Oct 6 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1684
by: Axel Dahmen | last post by:
Hi, when applying changes to the code and hitting to debug, a MessageBox pops up asking me if I want to build before debugging. Of course I want, that's why I've changed the code... Is there a simple way to suppress this MessageBox in Studio 7 and to start a Build-Debug sequence immediately? TIA, Axel Dahmen
0
2763
by: Vijay | last post by:
Hi, I have created an ActiveX ocx control in VC++6.0. When I run it in an ActiveX container of TSTCON32.exe. (the one that is shown while debugging) it runs perfectly. Iam trying to include it in a webpage (I am using visual-interdev for web page creation) by first customizing the toolbox for that ActiveX control and then dragging it to the web page from the toolbox. As soon as I drag it to the webpage, the IDE crashes with a message...
1
5865
by: Alexander Walker | last post by:
Hello I have recently published a web application using the "Publish Web Site" option of the solution explorer from Visual Studio 2005, I have published the website so that the pages could not be updated, do I need to set the web.config so that it contains <compilation defaultLanguage="c#" debug="false"> before I publish the web site or can I make this configuration change on the deployment machine after the site has been deployed? ...
3
3101
by: André | last post by:
Hi, I put that question already, but it's still not very clear to me, so ... Assume following option in web.config= debug="false" but in one aspx page (test.aspx) <%@ debug="true" ..%>
0
1113
by: svsujeet | last post by:
All, I am trying to find the DLL in windows that has the "Assertion Failed" resource string. Any ideas? Sujeet
1
5539
by: veerender | last post by:
Hello, When i tried to load an application from a CD following the Setup link, i get the below error: Debug Assertion Failed ! Program C:\Program~1\Common~1\instal~1\Driver\10\Intel3~1\Driver.Exe File: isctype.c Line: 68 Expression: (Unsigned)c+1)<=256 Can someone be kind to advice how i can successfully install this application on my PC which has Core 2 Duo E6600 Processor, 2 GB DDR memory & Xp Service Pack 2.
6
3233
by: haneeshkb | last post by:
I am getting this problem when I tried to build my borland c++ project. (compling and make doesn't giving any problem ) Fatal: Fatal: Assertion failed: !"How can you expand a vapor heap?" at "LMEM.C" Can anybody help me on this ?
0
1491
by: Andy B | last post by:
I installed the guidance package projects (smart devices, web client and service model projects). I tried them out and found they aren't for me. When I removed all the traces of them from my projects and tried to compile the site, it said build failed. The wierd funky part is no errors show up in the error list. If I remove the web deployment projects then the build completes without complaining. When I recreate the web deployment projects...
1
3869
by: =?Utf-8?B?RWluYXY=?= | last post by:
hi, im developing a dll app under vs2005 (sp1). when trying to load my exe app (which uses the dll) im getting the following err message: "the application failed to initialize properly (0xc0000005)" im using a third party lib (qt 3.3.8) to create the gui. although eliminating it did solve to problem. im running in debug mode. i've used depends.exe and made sure all the dlls depend on the debug runtime dlls all depend on MSVCP80D.DLL and...
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.