473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Pointers stored in the list getting corrupted??

Hi,
Have been struggling to find the reason for this occasional problem with pointers that I am storing in a list (STL C++).
My class name is : Tamper

All I do is this:

1- Instantiate Tamper with new and push the address in a list of type Tamper. The number of pushes are huge, may be in hundreds (dont think it matters as long as i have space).

Tamper *ptr=new Tamper();
list<Tamper *> = mylist;
mylist.push_back(ptr);

2- At a later point, I simply use list iterators and retrieve the stored pointers and call "delete" on them, which sounds straight forward and it works fine for me. But once in a while, I see that my process dumps core with SIGSEGV or SIGBUS, and stack points to "delete" operation.

list<Tamper *>::iterator it = mylist.begin();

for(; it != mylist.end() ; ++it)
{
delete *it;
*it=0;
}

How do I get rid of this occasional but consistent problem while freeing the memory. I don't think I am trying to release someone else's memory.

Appreciate your help here.
Jun 22 '11 #1
6 2459
weaknessforcats
9,208 Expert Mod 8TB
What you show is straightforward. You delete pointers you allocated and you set those deleted pointers to zero to avoid an exception by deleting them again.

Are you sure you never pushed a local pointer? That wil do it every time.

Is this a multi-threaded program? If so, the delete and he subsequent setting of the pointer to zero is no thread safe.
Jun 22 '11 #2
Thanks for your reply first of all!!!
This is not multithreaded, and there is no pushing of local pointer. I already did something like this just to make sure I am deleting my own pointers:

for(; it != mylist.end() ; ++it)
{
if ( typeid(*it) == typeid(Tamper *) )
{
delete *it;
*it=0;
}
}


This works in a similar way as before. But dumps core as usual.

The point here is, somehow the addresses are getting corrupted.
- Is there a better way to validate the address before deleting?
- Could there be environment issue here?
Jun 22 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
I ran your code for 1000 pointers and failed to produce a crash.

There's something else going on that's not in the code you have shown. Using naked pointers in C++ is very dangerous. The Tamper* should be a handle object.

There is no way to validate an address. The most you can do is say that a zero address is invalid. All non-zero addresses are presumed valid.

Is there by chance a delete of the Tamper* somewhere else in the code and at that place the pointer is not set to zero?
Jun 22 '11 #4
mac11
256 100+
I think you'd be better off having your list hold smart pointers (such as boost::shared_ptr). Then you don't have to explicitly delete anything and are much less likely to run into problems with raw pointers.

For an example see:
http://www.boost.org/doc/libs/1_46_1...tr_example.cpp
Jun 22 '11 #5
Thanks for your answer.
I will check and make sure I am not deleting this address somewhere else, though I am sure I am not doing it.
I will come back to you weaknessforcats after some more check.
Jun 23 '11 #6
Thanks mac11... I will check the possibility of using boost shared ptrs.
Jun 23 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shiv | last post by:
I have a servlet driven web application using ServletExec 4.2 AS, IIS 4.0 as the platform. Some user data is stored is persisted in a user object at the beginning of the session which includes use...
0
by: weldonj | last post by:
I upgraded to MySQL 4.1.1-alpha-standard the other night and ever since then at least twice a day my main message board table will crash. Here's what i get when I check on the board or analyze...
4
by: Paul Jackson | last post by:
I have the following situation: class Y; class Z; class X { public: Y y; Z* z; };
5
by: chirag | last post by:
hi i have hw assignment providing info below. please provide some code if you can. thanks. int lookupValue(List *listPtr, ListItemType item, bool &success) // If an entry equal to item exists...
2
by: TS | last post by:
I have some inner exceptions on dev and production server saying: Invalid character in a Base-64 string. OR Invalid Length of a Base-64 string or something like that. I cannot recreate this...
2
by: Sean | last post by:
Hi I have the following code: struct node{ int ID; bool connected; int *connectedTo; int numConnectedTo; int numMsgsRecieved; int overwhelmed; node *nxtNode;
1
by: Bizancio | last post by:
Im doing a program in C, using visual studio and i have to make a list of players (struct). I have a pointer that marks FIRST and LAST element of the list. The problem is that when i try to pass the...
5
by: abhi3211 | last post by:
i am using java inside java script page. in that page i want to use two dropdown list. in first dropdown list i am getting data from ms-access database. in second dropdown list i want to get data...
1
by: Rohith KS | last post by:
Hi i am using CDOSYS to send mails to the users from my application. Everything is working fine, mails are being sent to the user but in the mail the user receives, some extra characters such as...
0
by: Zogo | last post by:
Hi, I read a chinese character from the notepad using c++ in OMVS to pass it to the stored procedure in DB2. In the process, the three bytes chinese character is read byte by byte and so I get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
agi2029
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 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.