Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-02-2011, 08:56 PM   PM User | #1
Mihai Zaharescu
New Coder

 
Join Date: Mar 2011
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Mihai Zaharescu is an unknown quantity at this point
Unhappy C++ : "new" gives me the same pointer twice

Hi,
I'm not sure what's happening here:

Fereastra is an instance of fereastra allocated with new.

I don't remember deleting Fereastra. To be a bit more certain I used IsBadReadPtr to see if Fereastra is cleared, but it seams ok.

I want to allocate FereastraIcoana with new.

I end up with FereastraIcoana having the same address as Fereastra.

Code:
//test to see if Fereastra is ok	
if(IsBadReadPtr(Fereastra, 1))
{
	int i=0;    //breakpoint here, not entering
}

//allocate FereastraIcoana with new
FereastraIcoana=new fereastra(Fereastra->width()-2*bordura_icoana, Fereastra->height()-2*bordura_icoana);

//setting some values
FereastraIcoana->ascult_evenimente=false;
FereastraIcoana->move(bordura_icoana, bordura_icoana);
FereastraIcoana->SetMereuVizibila();

//here is the strange part, test to see if Fereastra (which was allocated sometime earlier) is at the same location as the newly allocated pointer
if(Fereastra==FereastraIcoana)
{
	int i=0;    //breakpoint here, enters here
}
no warnings, all values for Fereastra are valid and ok while debugging.

Do you have any ideas? It's the end of the day and I can't get any further with this error

I'm using Microsoft Visual c++ 2008

Thank you
Mihai Zaharescu is offline   Reply With Quote
Old 05-02-2011, 09:05 PM   PM User | #2
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Show where you allocate the memory for Fereastra and show us the implementation of IsBadReadPtr.

I would do more checking to be certain that you aren't deleting the Fereastra pointer at some point. You could put a breakpoint inside the destructor to know for sure.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 05-02-2011, 10:31 PM   PM User | #3
Mihai Zaharescu
New Coder

 
Join Date: Mar 2011
Posts: 25
Thanks: 1
Thanked 0 Times in 0 Posts
Mihai Zaharescu is an unknown quantity at this point
The code is to big to post here. That was my thought too, that I deleted the object somewhere... Thank you for the idea of a breakpoint inside the destructor, didn't think of that
That function (IsBadReadPtr) is a windows function. They say that it doesn't work for all pointers, but if it says it is bad, it is for sure bad

I don't know where the problem was, but I changed something from a vector of objects to a vector of pointers to objects and it seams to work now. Maybe the Fereastra class in which I am is read twice from that vectror... I hope it won't show up later...

Last edited by Mihai Zaharescu; 05-02-2011 at 10:33 PM..
Mihai Zaharescu is offline   Reply With Quote
Reply

Bookmarks

Tags
allocate, c++, duplicate pointer, new duplicate, reuse address

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:11 AM.


Advertisement
Log in to turn off these ads.