Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Same variable names inside multiple sub-scopes results in delete tocrash sporadically (http://www.velocityreviews.com/forums/t953070-same-variable-names-inside-multiple-sub-scopes-results-in-delete-tocrash-sporadically.html)

pvinodhkumar@gmail.com 10-05-2012 09:39 AM

Same variable names inside multiple sub-scopes results in delete tocrash sporadically
 
#include <stdio.h>
#include <string.h>

struct A
{
void* myPtr;
};

int main()
{
{
A a;
char* aPtr = new char[1024];
char* bPtr = aPtr;
a.myPtr = aPtr;

memset(aPtr, 'A', 1024);

// delete 1
delete aPtr;
}

{
A a;
char* aPtr = new char[1024];
char* bPtr = aPtr;
a.myPtr = aPtr;

memset(aPtr, 'A', 1024);

// delete 2
delete aPtr;
}
}

The delete2 crashes sometimes.

The compiler used is I use Microsoft Visual Studio 2008, with Service Pack1.

Any obvious reason for it to crash?

Stefan van Kessel 10-05-2012 09:45 AM

Re: Same variable names inside multiple sub-scopes results in deleteto crash sporadically
 
On 2012-10-05 11:39 AM, pvinodhkumar@gmail.com wrote:
> char* aPtr = new char[1024];
> delete aPtr;
>
> The delete2 crashes sometimes.


When you use new ... [] you need to use delete[] ...

pvinodhkumar@gmail.com 10-05-2012 10:13 AM

Re: Same variable names inside multiple sub-scopes results in deleteto crash sporadically
 
On Friday, October 5, 2012 3:15:34 PM UTC+5:30, Stefan van Kessel wrote:
> On 2012-10-05 11:39 AM, pvinodhkumar@gmail.com wrote:
>
> > char* aPtr = new char[1024];

>
> > delete aPtr;

>
> >

>
> > The delete2 crashes sometimes.

>
>
>
> When you use new ... [] you need to use delete[] ...


Thanks. You are Right. I used the right delete [] in actual code.
Still wondering why it is happening? My suspicion is MSVC not supporting too much of neting and same names in them

pvinodhkumar@gmail.com 10-05-2012 10:14 AM

Re: Same variable names inside multiple sub-scopes results in deleteto crash sporadically
 
On Friday, October 5, 2012 3:09:50 PM UTC+5:30, (unknown) wrote:
> #include <stdio.h>
>
> #include <string.h>
>
>
>
> struct A
>
> {
>
> void* myPtr;
>
> };
>
>
>
> int main()
>
> {
>
> {
>
> A a;
>
> char* aPtr = new char[1024];
>
> char* bPtr = aPtr;
>
> a.myPtr = aPtr;
>
>
>
> memset(aPtr, 'A', 1024);
>
>
>
> // delete 1
>
> delete aPtr;
>
> }
>
>
>
> {
>
> A a;
>
> char* aPtr = new char[1024];
>
> char* bPtr = aPtr;
>
> a.myPtr = aPtr;
>
>
>
> memset(aPtr, 'A', 1024);
>
>
>
> // delete 2
>
> delete aPtr;
>
> }
>
> }
>
>
>
> The delete2 crashes sometimes.
>
>
>
> The compiler used is I use Microsoft Visual Studio 2008, with Service Pack1.
>
>
>
> Any obvious reason for it to crash?


Please read my delete as delete [] . It happens when properly doing a delete [] as well.

Fred Zwarts \(KVI\) 10-05-2012 10:43 AM

Re: Same variable names inside multiple sub-scopes results in delete to crash sporadically
 
wrote in message
news:fe50b79c-1aa2-4772-b606-f75d8b46e0e5@googlegroups.com...
>
>On Friday, October 5, 2012 3:09:50 PM UTC+5:30, (unknown) wrote:
>> #include <stdio.h>
>>
>> #include <string.h>
>>
>>
>>
>> struct A
>>
>> {
>>
>> void* myPtr;
>>
>> };
>>
>>
>>
>> int main()
>>
>> {
>>
>> {
>>
>> A a;
>>
>> char* aPtr = new char[1024];
>>
>> char* bPtr = aPtr;
>>
>> a.myPtr = aPtr;
>>
>>
>>
>> memset(aPtr, 'A', 1024);
>>
>>
>>
>> // delete 1
>>
>> delete aPtr;
>>
>> }
>>
>>
>>
>> {
>>
>> A a;
>>
>> char* aPtr = new char[1024];
>>
>> char* bPtr = aPtr;
>>
>> a.myPtr = aPtr;
>>
>>
>>
>> memset(aPtr, 'A', 1024);
>>
>>
>>
>> // delete 2
>>
>> delete aPtr;
>>
>> }
>>
>> }
>>
>>
>>
>> The delete2 crashes sometimes.
>>
>>
>>
>> The compiler used is I use Microsoft Visual Studio 2008, with Service
>> Pack1.
>>
>>
>>
>> Any obvious reason for it to crash?

>
>Please read my delete as delete [] . It happens when properly doing a
>delete [] as well.


From this last remark, I conclude that the code that you posted is not the
actual code that crashes. Are there more differences? For example, did you
omit parts of struct A in this post? If A has a destructor that uses the
pointer, then it is clear that you delete the pointer before the destruction
of A, so that its destructor uses an invalid pointer.
For this reason, the FAQ tells you to post the exact code that reproduces
the problem.


Krice 10-05-2012 11:09 AM

Re: Same variable names inside multiple sub-scopes results in deleteto crash sporadically
 
On 5 loka, 12:39, pvinodhku...@gmail.com wrote:
> * * * * void* myPtr;


Don't do this in C++.

> * * * * * * * * char* bPtr = aPtr;


This is wrong too. Your code is crap, that's why is crashes.

pvinodhkumar@gmail.com 10-05-2012 11:36 AM

Re: Same variable names inside multiple sub-scopes results in deleteto crash sporadically
 
On Friday, October 5, 2012 4:13:35 PM UTC+5:30, F.Zwarts wrote:
> wrote in message
>
> news:fe50b79c-1aa2-4772-b606-f75d8b46e0e5@googlegroups.com...
>
> >

>
> >On Friday, October 5, 2012 3:09:50 PM UTC+5:30, (unknown) wrote:

>
> >> #include <stdio.h>

>
> >>

>
> >> #include <string.h>

>
> >>

>
> >>

>
> >>

>
> >> struct A

>
> >>

>
> >> {

>
> >>

>
> >> void* myPtr;

>
> >>

>
> >> };

>
> >>

>
> >>

>
> >>

>
> >> int main()

>
> >>

>
> >> {

>
> >>

>
> >> {

>
> >>

>
> >> A a;

>
> >>

>
> >> char* aPtr = new char[1024];

>
> >>

>
> >> char* bPtr = aPtr;

>
> >>

>
> >> a.myPtr = aPtr;

>
> >>

>
> >>

>
> >>

>
> >> memset(aPtr, 'A', 1024);

>
> >>

>
> >>

>
> >>

>
> >> // delete 1

>
> >>

>
> >> delete aPtr;

>
> >>

>
> >> }

>
> >>

>
> >>

>
> >>

>
> >> {

>
> >>

>
> >> A a;

>
> >>

>
> >> char* aPtr = new char[1024];

>
> >>

>
> >> char* bPtr = aPtr;

>
> >>

>
> >> a.myPtr = aPtr;

>
> >>

>
> >>

>
> >>

>
> >> memset(aPtr, 'A', 1024);

>
> >>

>
> >>

>
> >>

>
> >> // delete 2

>
> >>

>
> >> delete aPtr;

>
> >>

>
> >> }

>
> >>

>
> >> }

>
> >>

>
> >>

>
> >>

>
> >> The delete2 crashes sometimes.

>
> >>

>
> >>

>
> >>

>
> >> The compiler used is I use Microsoft Visual Studio 2008, with Service

>
> >> Pack1.

>
> >>

>
> >>

>
> >>

>
> >> Any obvious reason for it to crash?

>
> >

>
> >Please read my delete as delete [] . It happens when properly doing a

>
> >delete [] as well.

>
>
>
> From this last remark, I conclude that the code that you posted is not the
>
> actual code that crashes. Are there more differences? For example, did you
>
> omit parts of struct A in this post? If A has a destructor that uses the
>
> pointer, then it is clear that you delete the pointer before the destruction
>
> of A, so that its destructor uses an invalid pointer.
>
> For this reason, the FAQ tells you to post the exact code that reproduces
>
> the problem.


Thanks for the response. Except for my first typo everything is fine. Request everyone to make a note of it. Please let me know in case if there is a problem of same names and multiple scope or some other issue.

Thanks Again.

Øyvind Røtvold 10-05-2012 01:52 PM

Re: Same variable names inside multiple sub-scopes results in delete to crash sporadically
 
pvinodhkumar@gmail.com writes:

[ ... ]
>
> Thanks for the response. Except for my first typo everything is fine.


What's that suppose to mean? Is there still a problem with your code?
What is your code?

> Request everyone to make a note of it.


No, you post the exact code that produces this problem, the code
should be well formed as far as you can tell, and we will take it from
there.

> Please let me know in case if there is a problem of same names and
> multiple scope or some other issue.


If you think there's a problem with same names then just modify the
code to use different names and see if that removes the problem.



Also: If you've paid for this product (Visual Studio) and/or have
support for it then send the same well formed code to the supplier and
ask them about the issue. If you can expose a bug in their product
with this simple code they should appreciate that.


>
> Thanks Again.




--
... Øyvind - soon to appear in a kill file near you.
... Ignorance can be cured; stupidity is forever.

pvinodhkumar@gmail.com 10-06-2012 03:24 AM

Re: Same variable names inside multiple sub-scopes results in deleteto crash sporadically
 
On Friday, October 5, 2012 7:22:24 PM UTC+5:30, Øyvind Røtvold wrote:
> pvinodhkumar@gmail.com writes:
>
>
>
> [ ... ]
>
> >

>
> > Thanks for the response. Except for my first typo everything is fine.

>
>
>
> What's that suppose to mean? Is there still a problem with your code?
>
> What is your code?
>
>
>
> > Request everyone to make a note of it.

>
>
>
> No, you post the exact code that produces this problem, the code
>
> should be well formed as far as you can tell, and we will take it from
>
> there.
>
>
>
> > Please let me know in case if there is a problem of same names and

>
> > multiple scope or some other issue.

>
>
>
> If you think there's a problem with same names then just modify the
>
> code to use different names and see if that removes the problem.
>
>
>
>
>
>
>
> Also: If you've paid for this product (Visual Studio) and/or have
>
> support for it then send the same well formed code to the supplier and
>
> ask them about the issue. If you can expose a bug in their product
>
> with this simple code they should appreciate that.
>
>
>
>
>
> >

>
> > Thanks Again.

>
>
>
>
>
>
>
> --
>
> .. Øyvind - soon to appear in a kill file near you.
>
> .. Ignorance can be cured; stupidity is forever.


Hi Thanks for the reply. Yes, giving different names solves the problem in the production code. However, the problem does not occur when you have onlythis much in a program(i.e on a sample program). My program has thousands of lines of code and I am not able to post all those lines due to legal aspects with my employer.

That is why my suspicion is somehow the compiler has this same name bug in certain not so simple cases. Thanks so much.

pvinodhkumar@gmail.com 10-06-2012 03:27 AM

Re: Same variable names inside multiple sub-scopes results in deleteto crash sporadically
 
On Friday, October 5, 2012 7:00:10 PM UTC+5:30, Drew Lawson wrote:
> In article <f292c00d-769b-429c-89c6-8ef5163ab37d@googlegroups.com>
>
> pvinodhkumar@gmail.com writes:
>
>
>
> >Thanks for the response. Except for my first typo everything is fine.

>
> > Request everyone to make a note of it. Please let me

>
> >know in case if there is a problem of same names and multiple scope or some other issue.

>
>
>
> You are focusing on an assumed problem.
>
>
>
> The only problem with using the same symbol names in isolated
>
> contexts is the tendency to confuse the programmer. If the two
>
> contexts overlap in scope (one block inside another), that gets
>
> even more likely.
>
>
>
> If the code you posted, when it is the only thing in the program,
>
> crashes, then post again with the exact message/details/etc. Run
>
> it in a debugger and see what it says.
>
>
>
> I will note that the 'delete' lines are the last statements in code
>
> blocks with local variables. So the next thing after 'delete' is
>
> destroying the locals and (possibly) unwinding something on the
>
> stack. *That* may be where it is actually crashing. Try adding
>
> something like:
>
> std::cout << "After delete #1\n";
>
> and see if the crash is really on the delete.
>
>
>
> --
>
> Drew Lawson | We were taking a vote when
>
> | the ground came up and hit us.
>
> | -- Cylon warrior


The destructor is a trivial destructor in this case. However I will try let you know. Thanks so much for the response.


All times are GMT. The time now is 02:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.