472,110 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

EnterCriticalSection() is corrupting my heap

Hi all,

I have developed a static library which I am using in one of my application.
In my library I have created my own heap and all the objects (class objects)
in my application are created in that heap. What I have observed is that in
my library at a certain place when I call EnterCriticalSection() to an
object allocated at my heap, it is corrupting my heap. I am using Windows XP
and visual studio 8.0. Any hint how can I solve this problem?

Thanks,

Arsalan
May 24 '06 #1
9 2831
I have developed a static library which I am using in one of my application.
In my library I have created my own heap and all the objects (class objects)
in my application are created in that heap. What I have observed is that in
my library at a certain place when I call EnterCriticalSection() to an
object allocated at my heap, it is corrupting my heap. I am using Windows XP
and visual studio 8.0. Any hint how can I solve this problem?


How exactly do you create and use the heap?
Do you use HeapCreate/HeapAlloc/etc., or some other approach?
How do you detect the heap corruption?
How do you allocate memory for the CRITICAL_SECTION structure?
How do you pass this CRITICAL_SECTION to EnterCriticalSection?

Code samples would be helpful.

Regards,
Oleg
[VC++ MVP http://www.debuginfo.com/]

May 24 '06 #2
Hi,

Yes i use HeapCreate() and HeapAlloc().

I have a class object which is created on the heap and I have a member
variable in this class of type CRITICALSECTION (say m_cs). Inside one of my
class function when I call EnterCriticalSection(&m_cs) then this problem
occurs. Ok may be its not because of critical section because at the place
in code where EnterCriticalSection() was being called I create and
CAutoLock() object and pass my pointer to CRITICALSECTION object to it
(CAutoLock just call EnterCriticalSection in its constructor and
LeaveCriticalSection in its destructor). In the constructor when I try to
save pointer of critical section to the class member (CRITICALSECTION
*m_pCS) of CAutoLock then although it is pointer assignment but after
assignement the class member has some garbage data.

In my outside code:

{
CAutoLock(&m_cs);

// Some code
}

CAutoLock::CAutoLock(CRITICALSECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSection(m_pCS);
}

So any idea what is wrong?

Thanks,

Arsalan


Any idea whats wrong?
"Oleg Starodumov" <com-dot-debuginfo-at-oleg> wrote in message
news:es**************@TK2MSFTNGP03.phx.gbl...
I have developed a static library which I am using in one of my
application.
In my library I have created my own heap and all the objects (class
objects)
in my application are created in that heap. What I have observed is that
in
my library at a certain place when I call EnterCriticalSection() to an
object allocated at my heap, it is corrupting my heap. I am using Windows
XP
and visual studio 8.0. Any hint how can I solve this problem?


How exactly do you create and use the heap?
Do you use HeapCreate/HeapAlloc/etc., or some other approach?
How do you detect the heap corruption?
How do you allocate memory for the CRITICAL_SECTION structure?
How do you pass this CRITICAL_SECTION to EnterCriticalSection?

Code samples would be helpful.

Regards,
Oleg
[VC++ MVP http://www.debuginfo.com/]


May 24 '06 #3

In my outside code:

{
CAutoLock(&m_cs);

// Some code
}

CAutoLock::CAutoLock(CRITICALSECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSection(m_pCS);
}

So any idea what is wrong?


There can be a problem with the way the function is called (I mean the function
that instantiates CAutoLock object). It could be that it is called via a bad object
pointer, as a result "this" pointer passed to the function contains wrong value,
and so on. The next time you reproduce the problem, take a look at the value
of "this" passed to that function, and check if it's correct.

I mean something like this:

class CObj
{
...
CRITICAL_SECTION m_cs;
void YourFunc(); // instantiates CAutoLock and passes it &m_cs
}

CObj pObj; // not initialized, for example
pObj->YourFunc(); // when it is called, "this" pointer is bad, and thus pointers to
// its data members will also be bad

Generic safety checks for heap corruptions with PageHeap would not harm too,
try to enable it as described here:
http://www.debuginfo.com/tips/userbpntdll.html

Oleg


May 24 '06 #4
As far as CAutoLock is concerned, I am creating its object in stack as
follows:
{
CAutoLock lock(&m_cs);

// Some code
}

So still no idea what is wrong.

Regards,

Arsalan

"Oleg Starodumov" <com-dot-debuginfo-at-oleg> wrote in message
news:O6**************@TK2MSFTNGP04.phx.gbl...

In my outside code:

{
CAutoLock(&m_cs);

// Some code
}

CAutoLock::CAutoLock(CRITICALSECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSection(m_pCS);
}

So any idea what is wrong?


There can be a problem with the way the function is called (I mean the
function
that instantiates CAutoLock object). It could be that it is called via a
bad object
pointer, as a result "this" pointer passed to the function contains wrong
value,
and so on. The next time you reproduce the problem, take a look at the
value
of "this" passed to that function, and check if it's correct.

I mean something like this:

class CObj
{
...
CRITICAL_SECTION m_cs;
void YourFunc(); // instantiates CAutoLock and passes it &m_cs
}

CObj pObj; // not initialized, for example
pObj->YourFunc(); // when it is called, "this" pointer is bad, and thus
pointers to
// its data members will also be bad

Generic safety checks for heap corruptions with PageHeap would not harm
too,
try to enable it as described here:
http://www.debuginfo.com/tips/userbpntdll.html

Oleg


May 24 '06 #5
As far as CAutoLock is concerned, I am creating its object in stack as
follows:
{
CAutoLock lock(&m_cs);

// Some code
}


So still no idea what is wrong.


No, I mean the function that instantiates CAutoLock. E.g. if it is:

void SomeClass::SomeFunc()
{
CAutoLock lock(&m_cs);
// Some code
}

Check "this" pointer passed to SomeClass::SomeFunc, and how
the object of SomeClass is instantiated. (E.g. use Call Stack window
to activate the previous frame on the stack, and inspect "this"
in Watch window).

Oleg


May 24 '06 #6
Still no luck. Just one assignment statement inside the constructor (in
which i am assigning one pointer to another is not working) and if i dont
use CAutoLock class at all then at the same line when EnterCriticalSeciton()
statement executes another pointer gets garbage value. This is the time when
I really think about switching to .NET but I cannot :(.

Regards,

Arsalan
"Oleg Starodumov" <com-dot-debuginfo-at-oleg> wrote in message
news:OB**************@TK2MSFTNGP05.phx.gbl...
As far as CAutoLock is concerned, I am creating its object in stack as
follows:
>> {
>> CAutoLock lock(&m_cs);
>>
>> // Some code
>> }


So still no idea what is wrong.


No, I mean the function that instantiates CAutoLock. E.g. if it is:

void SomeClass::SomeFunc()
{
CAutoLock lock(&m_cs);
// Some code
}

Check "this" pointer passed to SomeClass::SomeFunc, and how
the object of SomeClass is instantiated. (E.g. use Call Stack window
to activate the previous frame on the stack, and inspect "this"
in Watch window).

Oleg

May 24 '06 #7
In message <eA**************@TK2MSFTNGP04.phx.gbl>, Arsalan Ahmad
<ar*****@hotmail.com> writes
CAutoLock::CAutoLock(CRITICALSECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSection(m_pCS);
}


Do you call InitializeCriticalSection() anywhere? You must call this
once before you try entering the critical section. Doesn't look to me
like you are using MFC's CCriticalSection wrapper, so I assume you are
using the raw Win32 object. Hence you need to initialize it.

VOID InitializeCriticalSection(
LPCRITICAL_SECTION lpCriticalSection // critical section
);

Thread Validator from Software Verification would have identified this
error if you had run your code through it.

http://www.softwareverify.com/thread...tor/index.html

Stephen
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk/software.html
Computer Consultancy, Software Development
Windows C++, Java, Assembler, Performance Analysis, Troubleshooting
May 24 '06 #8
Still no luck. Just one assignment statement inside the constructor (in
which i am assigning one pointer to another is not working) and if i dont use CAutoLock class at all then at the same
line when EnterCriticalSeciton() statement executes another pointer gets garbage value. This is the time when I really
think about switching to .NET but I cannot :(.


Run the application under debugger and stop at the following line:
{
CAutoLock lock(&m_cs); <== STOP HERE
// Some code
}


Enter "this" into Watch window. What value will be shown?

Then F11 (step into) CAutoLock constructor:

CAutoLock::CAutoLock(CRITICALSECTION *pCS)
{
m_pCS = pCS;
EnterCriticalSection(m_pCS); <== STOP HERE
}

Enter "m_pCS" into Watch window. What value will be shown?

Oleg


May 24 '06 #9
On Wed, 24 May 2006 15:19:27 +0200, "Arsalan Ahmad" <ar*****@hotmail.com>
wrote:
Hi,

Yes i use HeapCreate() and HeapAlloc().

I have a class object which is created on the heap and I have a member
variable in this class of type CRITICALSECTION (say m_cs). Inside one of my
class function when I call EnterCriticalSection(&m_cs) then this problem
occurs. Ok may be its not because of critical section because at the place
in code where EnterCriticalSection() was being called I create and
CAutoLock() object and pass my pointer to CRITICALSECTION object to it
(CAutoLock just call EnterCriticalSection in its constructor and
LeaveCriticalSection in its destructor). In the constructor when I try to
save pointer of critical section to the class member (CRITICALSECTION
*m_pCS) of CAutoLock then although it is pointer assignment but after
assignement the class member has some garbage data.

In my outside code:

{
CAutoLock(&m_cs);

// Some code
}

CAutoLock::CAutoLock(CRITICALSECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSection(m_pCS);
}

So any idea what is wrong?


I see you corrected the above in a subsequent message, but it's worth
mentioning that the following will indeed compile, but it won't do what you
want:

{
CAutoLock(&m_cs);

// Some code
}

This will just create a temporary CAutoLock object and immediately destroy
it, so the code which follows it in the block will not execute in a
critical section.

--
Doug Harrison
Visual C++ MVP
May 25 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by cppaddict | last post: by
9 posts views Thread by William L. Bahn | last post: by
16 posts views Thread by sarathy | last post: by
reply views Thread by JosAH | last post: by
4 posts views Thread by ggoubb | last post: by
reply views Thread by leo001 | last post: by

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.