473,802 Members | 2,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program layout in memory (is anything overwriting my static pointer?)

Hi,

I've been given the job of sorting out a crash in our product for
which we have the crash information and an avi of the event (which
can't possibly match but more of that later...) (btw this is a single
threaded VC9 / win32 app)

The call stack for the bug effectively goes

void* myBuf;

void myFunc()
{

if( myBuf )
{
(maybe some stuff happens here)
do something with myBuf.. except it's NULL in the debug
information I have
}

My question is.. can anything in the heap have overwritten myBuf with
NULL?
Another possibilty is another static variable, say a fixed size array
where we're writing outside the bounds.

My boss just told me we're using a memory manager called Smartheap,
crap knows what that's doing..

Any comments?

Oct 9 '08 #1
41 2857
simonl wrote:
Hi,

I've been given the job of sorting out a crash in our product for
which we have the crash information and an avi of the event (which
can't possibly match but more of that later...) (btw this is a single
threaded VC9 / win32 app)

The call stack for the bug effectively goes

void* myBuf;

void myFunc()
{

if( myBuf )
{
}

My question is.. can anything in the heap have overwritten myBuf with
NULL?
One the heap? If your application is threaded and writes to myBuf are
unguarded, anything might happen. myBuf isn't static, its global.

--
Ian Collins.
Oct 9 '08 #2
simonl wrote:
Hi,

I've been given the job of sorting out a crash in our product
for which we have the crash information and an avi of the event
(which can't possibly match but more of that later...) (btw
this is a single threaded VC9 / win32 app)

The call stack for the bug effectively goes

void* myBuf;

void myFunc()
{

if( myBuf )
{
(maybe some stuff happens here)
do something with myBuf.. except it's NULL in the debug
information I have
}

My question is.. can anything in the heap have overwritten
myBuf with NULL?
Another possibilty is another static variable, say a fixed size
array where we're writing outside the bounds.

My boss just told me we're using a memory manager called
Smartheap, crap knows what that's doing..

Any comments?
Testing for NULL != myBuf (note in C you should always test
against the NULL macro explicitly, it may be defined as not
being of value 0 on all systems, 0xffffffff may be as common),
just tells you if the pointer has been marked invalid/NIL
explicitly. It doesn't tell you however if it's valid.

You may have a perfectly non-NULL pointer, that's still not
pointing into valid memory.

Eventually you expect myBuf to be NULL if it's not initialized.
(Big) surprise though: Variables not being initialized
explicitly can have any value until getting a value assigned.

So add these two changes to your program, and see if it works
then:

- void* myBuf;
+ void *myBuf = NULL;

- if( myBuf )
+ if( NULL != myBuf )

And you might try test your program with a debugger, set a watch
on myBuf, to see when it changes.

Wolfgang Draxinger
--
E-Mail address works, Jabber: he******@jabber .org, ICQ: 134682867

Oct 9 '08 #3
Wolfgang Draxinger <wd********@dar kstargames.dewr ote:
Testing for NULL != myBuf (note in C you should always test
against the NULL macro explicitly, it may be defined as not
being of value 0 on all systems, 0xffffffff may be as common),
This is nonsense. A constant integer expression with the value zero
(such as, ooh, perhaps 0) will _always_ compare equal to a null pointer,
no matter how that null pointer is represented in memory; and a boolean
test against any scalar, including pointers, _always_ happens as if it
was written to include !=0.

Richard
Oct 9 '08 #4
>
>
myBuf isn't static, its global.
OK, I'm not too clear on this. Both global and static variables in
this case have the same scope and lifetime, but are stored in
different areas of process memory?
Oct 9 '08 #5
Ian Collins <ia******@hotma il.comwrites:
simonl wrote:
<snip>
>>... btw this is a single threaded VC9 / win32 app
<snip>
If your application is threaded and writes to myBuf are
unguarded, anything might happen.
I seems not.

--
Ben.
Oct 9 '08 #6
simonl <si*******@hotm ail.comwrites:
I've been given the job of sorting out a crash in our product for
which we have the crash information and an avi of the event (which
can't possibly match but more of that later...) (btw this is a single
threaded VC9 / win32 app)

The call stack for the bug effectively goes

void* myBuf;

void myFunc()
{

if( myBuf )
{
(maybe some stuff happens here)
do something with myBuf.. except it's NULL in the debug
information I have
}

My question is.. can anything in the heap have overwritten myBuf with
NULL?
Another possibilty is another static variable, say a fixed size array
where we're writing outside the bounds.
You need to ask yourself if the debug information is reliable. I have
no reason to doubt it, but do be sure. You can waste a lot of time
using unreliable information.

If the data is sound then something has altered myBuf between testing
it and the crash point where your debug data shows it to be NULL. The
most likely suspects are, as you say, an out of bounds access to a
nearly object, but another possibility is access though an invalid
pointer. Is there a lot of code being executed between the if and the
reported crash? Can you reproduce the error or must you debug this
fro one debug data set?
My boss just told me we're using a memory manager called Smartheap,
crap knows what that's doing..
I don't like the sound of that, but I know nothing about it.

--
Ben.
Oct 9 '08 #7
Wolfgang Draxinger wrote:
simonl wrote:
(fx:snip)
>void myFunc()
{

if( myBuf )
{
(maybe some stuff happens here)
do something with myBuf.. except it's NULL in the debug
information I have
}

My question is.. can anything in the heap have overwritten
myBuf with NULL?
Another possibilty is another static variable, say a fixed size
array where we're writing outside the bounds.

My boss just told me we're using a memory manager called
Smartheap, crap knows what that's doing..

Any comments?

Testing for NULL != myBuf (note in C you should always test
against the NULL macro explicitly,
False.
it may be defined as not
being of value 0 on all systems, 0xffffffff may be as common),
True but irrelevant; null pointers are /required/ to behave
like 0 in a condition, just as they are /required/ to compare
equal to null pointer constants in comparisions. If the bitwise
representation of a null pointer is 0xffffffff, then the compiler
must insert the appropriate code, just as it must for

int *mylittlenullpo inter = 0;
Eventually you expect myBuf to be NULL if it's not initialized.
(Big) surprise though: Variables not being initialized
explicitly can have any value until getting a value assigned.
Not static variables; they are implicitly initialised to
suitable zeros. So, if they're pointers, they get initialsed
the null pointer.

--
'It changed the future .. and it changed us.' /Babylon 5/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Oct 9 '08 #8
On Thu, 09 Oct 2008 12:14:07 +0200, Wolfgang Draxinger wrote:
Testing for NULL != myBuf (note in C you should always test against the
NULL macro explicitly, it may be defined as not being of value 0 on all
systems, 0xffffffff may be as common)
Since:

if( NULL == ptr )

is always exactly equivalent to:

if( ! ptr )

I prefer:

if( !! ptr )

to

if( NULL != ptr )

the two are equivalent.
Oct 9 '08 #9
viza <to******@gm-il.com.obviousc hange.invalidwr ote:
On Thu, 09 Oct 2008 12:14:07 +0200, Wolfgang Draxinger wrote:
Testing for NULL != myBuf (note in C you should always test against the
NULL macro explicitly, it may be defined as not being of value 0 on all
systems, 0xffffffff may be as common)

Since:

if( NULL == ptr )

is always exactly equivalent to:

if( ! ptr )

I prefer:

if( !! ptr )

to

if( NULL != ptr )

the two are equivalent.
And surprise, surprise, they're both also equivalent to the superior

if (ptr)

Richard
Oct 9 '08 #10

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

Similar topics

3
2671
by: growse | last post by:
Right, I've got a 2 c# programs here. Lets call them A and B. My aim is to send a simple string from B to A. A is always running. I've overridden the WndProc method to give me messages that are sent to it. B is a program that loads, sends a message and then quits. Let me give you the code to B (bits are missed out, but I've got the important stuff there): private const uint WM_USER_SENDTEXT = 0x8001;
11
4532
by: Henryk | last post by:
I have something like class Params { public: const static char nOne = 1; const static int nTwo = 2; const static char nThree = 3; }; This is just a wrapper for globally used parameters in an embedded
0
9561
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
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...
0
10058
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7597
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
6835
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
5494
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
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
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
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.