Click here to Skip to main content
15,902,189 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: random numbers rand() [edited] Pin
Jamie Hale25-Jun-03 9:16
Jamie Hale25-Jun-03 9:16 
GeneralExecutable Creation time Pin
gbenguita25-Jun-03 6:27
gbenguita25-Jun-03 6:27 
GeneralRe: Executable Creation time Pin
Rob Caldecott25-Jun-03 6:31
Rob Caldecott25-Jun-03 6:31 
GeneralHelp with EDCN2000N CCD Camera Software.... Pin
tonicito25-Jun-03 6:25
tonicito25-Jun-03 6:25 
GeneralRe: Help with EDCN2000N CCD Camera Software.... Pin
tonicito27-Jun-03 5:24
tonicito27-Jun-03 5:24 
Questionbug or my fault? Pin
nothingremained25-Jun-03 6:22
nothingremained25-Jun-03 6:22 
AnswerRe: bug or my fault? Pin
David Crow25-Jun-03 7:08
David Crow25-Jun-03 7:08 
QuestionMemory leak in CMapStringToOb - Why? Pin
pf725-Jun-03 5:53
pf725-Jun-03 5:53 
Hello you out there,

I am currently developing a tiny database program and therefore using a CMapStringToOb to store my user-defined objects in memory. During development, I suddenly faced a strange problem concerning memory usage and memory leaks. I thought that something was wrong with my class and therefore created a small class called CTest, which in fact is only a "box" or an int, I know it's silly, but, hey, it's at least maybe more easy to understand than a class with CStrings and so on...

<br />
class CTest : public CObject<br />
{<br />
private:<br />
int m_TestInt;<br />
<br />
public:<br />
CTest() { m_TestInt = 0; }<br />
CTest(int NewTestInt) { m_TestInt = NewTestInt; }<br />
~CTest() {}<br />
void SetInt(int NewTestInt) { m_TestInt = NewTestInt; }<br />
int GetInt() const { return m_TestInt; }<br />
};<br />


Then I'm using it with my CMapStringToOb:

<br />
CMapStringToOb *pTestObs = new CMapStringToOb; // Create CMapStringToOb on heap (at least I thing so?!)<br />
pTestObs->InitHashTable(50000,TRUE); // Init the hash table to work faster and allocate memory now<br />
<br />
char buffer[6];<br />
<br />
for(int i = i; i<= 49999; i++)<br />
{<br />
pTestObs->SetAt(_itoa(i,Buffer,10), new CTest(i));<br />
}<br />


Ok, up to here, everything works fine. I can retrieve my elements by calling "Lookup" and so on.
But now, please have a look at that piece of code in which I'm trying to delete my objects as well as the pointers and the "Map" as stated in the MSDN:

<br />
POSITION pos = pTestObs->GetStartPosition(); // Get starting position to iterate the map<br />
<br />
while(pos != NULL)<br />
{<br />
CString String;<br />
CTest* pTest = NULL;<br />
pTestObs->GetNextAssoc(pos, String, (CObject*&)pTest);<br />
pTestObs->RemoveKey(String);<br />
delete pTest;<br />
}<br />
<br />
// pTestObs->RemoveAll(); || this didn't help either!<br />
<br />
delete pTestObs;<br />


The problem with this piece of code now is that, in Debug mode, everything seems to work just fine, I added some ::AfxMessageBox(...); statements in order to know what goes right and what goes wrong.
These CObjects need roughly 7 MB of RAM on my machine. In Debug mode, the upper code frees 7 MB, as previously needed and allocated. But in Release mode, after creating these objects and inserting them into the CMapStringToOb, this part of the code only frees round 3 MB of the memory. But... where is the rest?! I am leaking 4 MB of memory and dealing with much larger objects in the REAL program, which might easily mean around 20 MBytes.

Can any one help me? Does anyone know what I am doing wrong?

Best regards and thanks in advance,
pf7


(@ All Americans: I live in Germany, so please do not think that I won't reply immediately. It's just because of the difference in time. I'll reply!)
AnswerRe: Memory leak in CMapStringToOb - Why? Pin
pf725-Jun-03 8:45
pf725-Jun-03 8:45 
GeneralRe: Memory leak in CMapStringToOb - Why? Pin
basementman25-Jun-03 11:27
basementman25-Jun-03 11:27 
GeneralRe: Memory leak in CMapStringToOb - Why? Pin
pf725-Jun-03 11:58
pf725-Jun-03 11:58 
GeneralRe: Memory leak in CMapStringToOb - Why? Pin
pf726-Jun-03 3:52
pf726-Jun-03 3:52 
GeneralRe: Memory leak in CMapStringToOb - Why? Pin
John M. Drescher26-Jun-03 23:27
John M. Drescher26-Jun-03 23:27 
GeneralRe: Memory leak in CMapStringToOb - Why? Pin
pf727-Jun-03 4:17
pf727-Jun-03 4:17 
GeneralRe: Memory leak in CMapStringToOb - Why? Pin
John M. Drescher27-Jun-03 9:47
John M. Drescher27-Jun-03 9:47 
GeneralRe: Memory leak in CMapStringToOb - Why? Pin
pf727-Jun-03 11:29
pf727-Jun-03 11:29 
Generalmaximum characters in edit box Pin
skinnyreptile25-Jun-03 5:36
skinnyreptile25-Jun-03 5:36 
GeneralRe: maximum characters in edit box Pin
Rob Caldecott25-Jun-03 5:40
Rob Caldecott25-Jun-03 5:40 
GeneralRe: maximum characters in edit box Pin
skinnyreptile25-Jun-03 5:48
skinnyreptile25-Jun-03 5:48 
GeneralRTTI/dynamic_cast Pin
Rob Caldecott25-Jun-03 5:08
Rob Caldecott25-Jun-03 5:08 
GeneralRe: RTTI/dynamic_cast Pin
John M. Drescher25-Jun-03 5:13
John M. Drescher25-Jun-03 5:13 
GeneralRe: RTTI/dynamic_cast Pin
Nemanja Trifunovic25-Jun-03 5:29
Nemanja Trifunovic25-Jun-03 5:29 
GeneralRe: RTTI/dynamic_cast Pin
Rob Caldecott25-Jun-03 5:33
Rob Caldecott25-Jun-03 5:33 
GeneralRe: RTTI/dynamic_cast Pin
peterchen25-Jun-03 5:33
peterchen25-Jun-03 5:33 
GeneralRe: RTTI/dynamic_cast Pin
Peter Weyzen25-Jun-03 5:49
Peter Weyzen25-Jun-03 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.