473,573 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

EnterCriticalSe ction() 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 EnterCriticalSe ction() 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 2919
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 EnterCriticalSe ction() 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_SECTIO N structure?
How do you pass this CRITICAL_SECTIO N to EnterCriticalSe ction?

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 EnterCriticalSe ction(&m_cs) then this problem
occurs. Ok may be its not because of critical section because at the place
in code where EnterCriticalSe ction() was being called I create and
CAutoLock() object and pass my pointer to CRITICALSECTION object to it
(CAutoLock just call EnterCriticalSe ction in its constructor and
LeaveCriticalSe ction in its destructor). In the constructor when I try to
save pointer of critical section to the class member (CRITICALSECTIO N
*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::CAut oLock(CRITICALS ECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSe ction(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******** ******@TK2MSFTN GP03.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 EnterCriticalSe ction() 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_SECTIO N structure?
How do you pass this CRITICAL_SECTIO N to EnterCriticalSe ction?

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::CAut oLock(CRITICALS ECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSe ction(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_SECTIO N 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******** ******@TK2MSFTN GP04.phx.gbl...

In my outside code:

{
CAutoLock(&m_cs );

// Some code
}

CAutoLock::CAut oLock(CRITICALS ECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSe ction(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_SECTIO N 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::Some Func()
{
CAutoLock lock(&m_cs);
// Some code
}

Check "this" pointer passed to SomeClass::Some Func, 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 EnterCriticalSe citon()
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******** ******@TK2MSFTN GP05.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::Some Func()
{
CAutoLock lock(&m_cs);
// Some code
}

Check "this" pointer passed to SomeClass::Some Func, 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*****@hotmai l.com> writes
CAutoLock::CAu toLock(CRITICAL SECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSe ction(m_pCS);
}


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

VOID InitializeCriti calSection(
LPCRITICAL_SECT ION lpCriticalSecti on // 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 EnterCriticalSe citon() 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::CAut oLock(CRITICALS ECTION *pCS)
{
m_pCS = pCS;
EnterCriticalSe ction(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*****@hotmai l.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 EnterCriticalSe ction(&m_cs) then this problem
occurs. Ok may be its not because of critical section because at the place
in code where EnterCriticalSe ction() was being called I create and
CAutoLock() object and pass my pointer to CRITICALSECTION object to it
(CAutoLock just call EnterCriticalSe ction in its constructor and
LeaveCriticalS ection in its destructor). In the constructor when I try to
save pointer of critical section to the class member (CRITICALSECTIO N
*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::CAu toLock(CRITICAL SECTION *pCS)
{
m_pCS = pCS; <= This assignement is not working correctly and after
assignment m_pCS points to some garbage memory location
EnterCriticalSe ction(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5876
by: ANt | last post by:
Hi, we have some major GC issues at present with a system we're trying to put live. It's a live calculation engine that's distributed across about 30 Java server processes. A set of processes called objectservers sit between the core infra and a Sybase DB. Two of the main objectservers are set up with 2Gb Heaps as they need to cache large...
7
1444
by: cppaddict | last post by:
I'm running a Java application that uses a native C++ library I've written. The Java application hits one of the native methods pretty frequently (about 100 ms or so), and while it usually works fine, sometimes the program crashes with an error message about the heap. The method which seems to be causing the problem is very simple, and...
13
8238
by: PamelaDV | last post by:
I have a database split for back end and front end and my back end (my data) has been corrupting like crazy lately. Today we have compacted and repaired like 4 times within an hour. The database is not experiencing a lot of bloat. We have had instances in the past where the database has gotten up to 200,00KB before indicating it needed to...
9
2652
by: William L. Bahn | last post by:
I am having a strange problem - and I suspect it is something specific to my compiler - but want to get a read on the Standard C portion of it first. Basic Question: Under the C Standard, under what conditions, if any, can a value returned by malloc() not be used in a subsequent free() call? I have a program that does a lot of things. I...
2
6376
by: afatdog | last post by:
How to use EnterCriticalSection and LeaveCriticalSection in C#? How to define CRITICAL_SECTION?
16
4432
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If...
0
13287
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. Examples of two main problem scenarios are like this: 1) an array containing a first name and another array containing a last name and possibly a...
5
24642
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day...
4
9573
by: ggoubb | last post by:
The purpose of the Insert function is to add a new integer in the Heap assuming that it is not already full. If Heap capacity has been reached, it attempts to double the current capacity. If capacity cannot be doubled, it throws FullHeap. Here is the Heap.h file const int MAXSIZE = 4; // Default maximum heap size class...
0
7705
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...
0
8032
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8074
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...
1
5601
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...
0
5294
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...
0
3734
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...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2223
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
1
1310
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.