Hi all,
I have a class say A and created a pointer to call this class like
A *obj=NULL;
obj=new A( );
After I perform all the operations within the code and try to delete this pointer using
delete obj;
obj=NULL;
I get an exception as
" the instruction at "0x7c910f 2b" REFERENCED MEMORY AT "0x00000004".the memory could not be "read".Click OK to terminate the program.
I am not able to figure it out.
If i dont delete it the code runs smoothly.But always have to delete after use once allocated.The whole code is within a function block.
Could anyone please suggest me.
Thanks .
9 1658
Try removing the obj=NULL; portion and see if that works?
delete obj;
obj=NULL;
So you're deleting obj and then trying to assign it a NULL value?
Post the code of the destructor.
Have you stepped through your destructor with your debugger??
I removed NULL. It did not work.
I have my code in this way:
my own class - ///abc.h
-
-
class abc
-
{
-
int off;
-
int *img;
-
abc(void);
-
~abc( );
-
void sum( );
-
};
-
-
//abc.cpp
-
-
abc::abc(void)
-
{
-
off=0;
-
}
-
abc::~abc( )
-
{
-
delete[] img;
-
}
-
-
void abc::sum( )
-
{
-
Filter *f; //class already created by someone and I am using all its functions
-
f=new Filter ( );
-
std::string filename=f->datafile();
-
raw *r; //another class same like f created by someone.
-
r=new raw( );
-
r->add(filename );
-
img=new float[100];
-
delete r;
-
r=NULL;
-
delete f;
-
f=NULL;
-
}
I am getting that exception just before add function .I get the filename correctly from the class but when I pass to the add( ) that filename is corrupted and I see it as Bad Ptr when I stepped in thru the debugger step by step. when I delete r, I dont get any exception.
It is little bit confusing.Please let me now if any questions.I am still trying to figure it out.
Filter *f; //class already created by someone and I am using all its functions
f=new Filter ( );
This should be: -
Filter *f; //class already created by someone and I am using all its functions
-
f=new Filter;
-
Also, your abc class:
class abc
{
int off;
int *img;
abc(void);
~abc( );
void sum( );
};
Has no constructor. Therefore no guarantee that the img pointer ever got initialized. If you delete and uninitialized pointer, you crash.
The best way to find it is to start by adding error checking. For example: -
abc::abc()
-
{
-
off=0;
-
img = NULL;
-
}
-
...
-
abc::~abc()
-
{
-
if (img) {
-
delete(img);
-
}
-
img = NULL;
-
}
-
Adding the code will benefit you in the long run because it will catch the problem every time - even if it shouldn't happen any more.
I'm pretty sure that -
std::string filename=f->datafile();
-
copies the return value of datafile() then it gets copied again on the way in to add(). If you meant to get a pointer or a reference then make sure that's what's happening. For example, a pointer allocated on the stack inside datafile() won't be valid after that function returns. I'm always fuzzy on these things in C++ though.
The best way to find it is to start by adding error checking. For example:
Code: ( cpp )
abc::abc()
{
off=0;
img = NULL;
}
...
abc::~abc()
{
if (img) { <<<<<<<<<<<<<<<<<<<<<<<<<<
delete(img);
}
img = NULL;
}
Pointless test. delete does nothing if the pointer is null. However, setting the pointer to null after the delete is a good idea. This is different from C. There, free() crashes on a null pointer.
For example, a pointer allocated on the stack inside datafile() won't be valid after that function returns. I'm always fuzzy on these things in C++ though.
It's not the pointer that's the problem. It's the address inside the pointer. If that address is to another local variable, then you have a problem. But if the address is to something on the heap, then that address will be valid until you delete it.
Rob Russell had the right idea by setting img = NULL in the constructor. This is what was missing in the original constructor. When the object was created, img had a non-zero value which caused delete to crash and burn.
A minor point, in C++, NULL has fallen out of favor. Using img = 0 is preferable.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave |
last post by:
Hello all,
In the code below, I see the following output:
base::operator new(size_t, int)
base::base()
base::~base()
base::operator delete(void *)
In the case of an exception being thrown...
|
by: Nimmi Srivastav |
last post by:
There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents an excellent
discussion of overloaded new and delete operators. In fact there...
|
by: |
last post by:
Hi all,
I've been using C++ for quite a while now and I've come to the point where I need to overload new
and delete inorder to track memory and probably some profiling stuff too. I know that...
|
by: Ioannis Vranos |
last post by:
When we use the standard placement new operator provided in <new>, and
not a definition of owr own, isn't a call to placement delete enough?
Consider the code:
#include <new>
|
by: Jonan |
last post by:
Hello,
For several reasons I want to replace the built-in memory management with
some custom built. The mem management itlsef is not subject to my question -
it's ok to the point that I have...
|
by: Dave |
last post by:
Hello all,
I'd like to find a source on the web that discusses, in a comprehensive
manner and in one place, everything about new / delete. It should include
overloading operator new, the new...
|
by: silver360 |
last post by:
Hello,
I'm trying to create a basic Heap manager and i have some question
about new/delete overloading.
The following code give me this output :
>> $./heap
>> registered : 0x804d098
>>...
|
by: junw2000 |
last post by:
I use the code below to study delete and destructor.
#include <iostream>
using namespace std;
struct A {
virtual ~A() { cout << "~A()" << endl; }; //LINE1
void operator delete(void* p) {...
|
by: jeffjohnson_alpha |
last post by:
We all know that a new-expression,
foo* a = new foo() ;
allocates memory for a single foo then calls foo::foo(). And we know
that
void* p = ::operator new(sizeof(foo)) ;
allocates a...
|
by: mail.dsp |
last post by:
Suppose in a class we overload four operators:
operator new
operator delete
operator new
operator delete
class Test{
public:
void * operator new (size_t t){
cout<<"\nCalling... new";
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |