473,883 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory leak problems

Ok, now I'm officially confused. I have a large project going, which
uses a win32 ui library I am developing myself. And I'm getting weird
memory leaks. I don't know if I can explain what is going on, but I
really need some help on this one.

Ok, so I have this class defined (written by Randy Charles Morin,
www.kbcafe.com) which detects memory leaks. It creates a memory check
point in the constructor, and another in the destructor, and then
compares the two to find differences. When I want to test for memory
leaks I make a variable with this type at the global level (before
everything else). My thinking is the first-in last-out thing with
constructors and destructors. The first thing to be called would be this
object's constructor, and the last thing would be the destructor. Am I
right?

Ok; so my application Main looks like this:

int __stdcall WinMain(HINSTAN CE hInstance, HINSTANCE hPrevInstance, char
*lpCmdLine, int nCmdShow)
{
CWin32Applicati on *app;
app = MemNew CWin32Applicati on();
int ret = __Win32_RunMain App(app, hInstance, hPrevInstance, lpCmdLine,
nCmdShow);
app->Destroy();
MemDelete app;

return ret;
}

Never mind the weird __Win32_RunMain App, it's really just a temporary
throw-in which collects the command line and runs a Main function in
CWin32Applicati on. I will change this to the better, I swear! :-)

Ok, so the application is allocated, and a function is run which creates
windows, menus, toolbars, controls, documents etc etc etc. Here comes
the weird parts: If I place the control destruction calls in the
destructor of CWin32Applicati on, or in the function "Destroy" as you see
above (app->Destroy()), I get a memory leak. But if I put it in the code
which is called on the win32 message WM_DESTROY, I get NO memory leaks.
What the (insert curse word here) is going on? I'm not really allowed to
put their destruction calls in the WM_DESTROY function, so I can't use
that as a solution. As far as I know, every destruction function for
every control is actually called, I've stepped through the call. Still
memory leaks gets reported.

For the record, I also tried placing the memory leak class variable
within the WinMain itself, but it gave me exactly the same results.

I must admit, I will be very impressed if any of you can help me with
this, but I'm desperate.

Yours,
Morten Aune Lyrstad
Jul 22 '05 #1
4 2357
Morten Aune Lyrstad wrote:
Ok, now I'm officially confused. I have a large project going, which
uses a win32 ui library I am developing myself. And I'm getting weird
memory leaks. I don't know if I can explain what is going on, but I
really need some help on this one.

Ok, so I have this class defined (written by Randy Charles Morin,
www.kbcafe.com) which detects memory leaks. It creates a memory check
point in the constructor, and another in the destructor, and then
compares the two to find differences. When I want to test for memory
leaks I make a variable with this type at the global level (before
everything else). My thinking is the first-in last-out thing with
constructors and destructors. The first thing to be called would be this
object's constructor, and the last thing would be the destructor. Am I
right?
Could be. Or could be not. If other modules have namespace level objects
defined, they may be created before and destroyed after. It has no effect
on memory leak reporting, though.
Ok; so my application Main looks like this:

int __stdcall WinMain(HINSTAN CE hInstance, HINSTANCE hPrevInstance, char
*lpCmdLine, int nCmdShow)
{
CWin32Applicati on *app;
app = MemNew CWin32Applicati on();
Why not initialise it right away, where you declare it? Never mind... I
suppose 'MemNew' is some kind of a preprocessor wrapper for 'new'.
int ret = __Win32_RunMain App(app, hInstance, hPrevInstance,
lpCmdLine, nCmdShow);
app->Destroy();
MemDelete app;
I bet this is just 'delete app;'.

return ret;
}

Never mind the weird __Win32_RunMain App, it's really just a temporary
throw-in which collects the command line and runs a Main function in
CWin32Applicati on. I will change this to the better, I swear! :-)
And drop the double underscores while you're at it. You as a programmer
are prohibited from using double underscores in your names.
Ok, so the application is allocated, and a function is run which creates
windows, menus, toolbars, controls, documents etc etc etc. Here comes
the weird parts: If I place the control destruction calls in the
destructor of CWin32Applicati on, or in the function "Destroy" as you see
above (app->Destroy()), I get a memory leak. But if I put it in the code
which is called on the win32 message WM_DESTROY, I get NO memory leaks.
What the (insert curse word here) is going on?
Well, can you see if your code (regardless of where you put it) does get
called?
I'm not really allowed to
put their destruction calls in the WM_DESTROY function, so I can't use
that as a solution. As far as I know, every destruction function for
every control is actually called, I've stepped through the call. Still
memory leaks gets reported.
It could simply be a bug in the leak reporting tool you're using.
For the record, I also tried placing the memory leak class variable
within the WinMain itself, but it gave me exactly the same results.
That's good. It means that the c-tor/d-tor for it work as you thought
they did.
I must admit, I will be very impressed if any of you can help me with
this, but I'm desperate.


There is probably something OS-specific going on there. The procedures
calls to which you're moving from one place to the other to see that
difference in memory leak reporting are probably behaving differently
_themselves_ if you try to destroy something that has already been removed
by the OS (the WM_DESTROY message probably causes all the windows to close
and your code that frees up some resources may simply short-circuit out of
the deallocation procedures if it sees that there are no windows left).

In any case, it seems like a Windows programming problem, not a C++
language problem, so you're in a wrong newsgroup. As much as some of us
would love to help you, we can't do it here. It would be off-topic.

Try comp.os.ms-windows.program mer.* hierarchy or one of the newsgroups
on msnews.microsof t.com server.

Best of luck!

Victor
Jul 22 '05 #2
Well, I've actually been able to solve it, and it was a mixture of
win32api-specific and c++ specific things. Suffice to say, the right
code was being called at the wrong time. Some bad thinking while I was
developing made me connect certain objects to the windows they should
work with, which made it impossible to retrieve them again after the
windows were destroyed. Unfortunately, the fix for this problem
introduced another one, which I will be asking about in a new thread.

Oh, about the underscores thing... I used them so I would be guaranteed
to notice it each time I see them. Stupid solution, fine, but it
works... And I will fix it, once these other more pressing problems are
solved.

I am self-taught (or whatever it is called), so I'm sure I've got a
gazillion bad habits. As long as it's only me who has to live with the
code, it doesn't matter. The problem is, I'm studying to become a
professional programmer, which means that I have to keep a sharp eye out
for such things... so I appreciate your comment about it!

It's too bad that people get so hung up into if it's the right newsgroup
or not. It's hard to determine if such problems are c++-specific,
os-specific or whatever. No offense intended, I'm sure there are
perfectly good reasons for this, but it makes it a bit harder for people
like me to find the right place to ask the questions.

Thank you very much!
Morten Aune Lyrstad
Jul 22 '05 #3
> I am self-taught (or whatever it is called), so I'm sure I've got a
gazillion bad habits. As long as it's only me who has to live with the
code, it doesn't matter. The problem is, I'm studying to become a
professional programmer, which means that I have to keep a sharp eye out
for such things... so I appreciate your comment about it!


Get hold of all 3 books of Scott Meyers:
Effective C++
More Effective C++
Effective STL

Also Steve Dewhurst's 100 Gotchas

They will knock the bad habits out of you.
And stick around this newsgroup for a while.

Stephen Howe
Jul 22 '05 #4
KB
Morten Aune Lyrstad wrote:
I am self-taught (or whatever it is called), so I'm sure I've got a
gazillion bad habits. As long as it's only me who has to live with the
code, it doesn't matter. The problem is, I'm studying to become a
professional programmer, which means that I have to keep a sharp eye out
for such things... so I appreciate your comment about it!
Just keep at it. None of this stuff comes overnight.


It's too bad that people get so hung up into if it's the right newsgroup
or not. It's hard to determine if such problems are c++-specific,
os-specific or whatever. No offense intended, I'm sure there are
perfectly good reasons for this, but it makes it a bit harder for people
like me to find the right place to ask the questions.


Just read the FAQ and don't mention "gcc" even if you're stating whether it
possesses a std C++ compliant feature.

KPB
Jul 22 '05 #5

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

Similar topics

3
2042
by: Guy | last post by:
Hi It might take me a little time to explain this but here goes. Firstly I'm not using the latest upto date python releases so my first plan is to try more upto date rels of python and win32 libs, as this has fixed problems for me in the past (Don't like doing this as it usally breaks stuff and sys admin are slow to update software, although they do a cracking job, just covering my back.) I've created a build script for my companies...
32
3878
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
10
1948
by: Jonathan Ames | last post by:
Moving to C++ from Java, I'm still confused by some aspects of memory cleanup operations. For example, let's say I have a class MovingObject which maintains a pointer to another class MovementAlgorithm. MovingObject has a method SetMovementAlgorithm(MovementAlgorithm* movementAlgorithm) if the body of this method reads
10
14067
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile file to use purify. However, my code is a rather simple single-source C program, and I didn't write a Makefile for it. I'm wondering if anybody can tell me which commands are to be entered at the Unix prompt to use purify. And, I don't know if...
13
1558
by: Boni | last post by:
I use 3-d party component. In this component I must pass a reference to my object. The problem is that this component has an ugly bug.When this component is disposed, it incorrectly don't delete the reference to my object from one of its shared lists.And since the operation repeats many times the leak is huge. Is there a way to kill my object anyway? Thanks a lot, Boni
2
2379
by: Robert | last post by:
Hello javascript group readers, I have a question regarding how to prevent memory leaks in Internet Explorer when using closures. I already knew about the circular reference problem, and until now was able to prevent memory leak problems. But I needed to store DOM elements and can't solve it anymore. So I search the group archive to see if I missed any valuable information. I found some interesting articles, but somehow could not apply...
7
15723
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a lot of memory but I still had to use it. 1. After using DLLImport and seeing the memory leak I tried to load and
17
2556
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. Thanks in advance,
9
4223
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It starts off at a nice 4% of memory, then slowly grows up to 50% and beyond. This translates to around 2 gigs of physical memory, and that's really way more memory than this program should be taking up.
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9778
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
10730
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
9559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7114
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
5784
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
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3228
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.