Jul 26, 2008

Free Memory After Out Of Scope?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Free Memory After Out Of Scope?

kvarnerexpress
Hi everyone,

I'm working with stl maps, I seem to be experiencing a memory leak somewhere, the debugger isn't giving me any warning about it though. I thought maps and other containers are supposed to free themselves once they fall out of scope?


Code:
void function1()
{
map<CString, set<CString> > local_map;

// insert some stuff into local_map...
local_map["key"].insert("somethin");
local_map["key"].insert("somethin else");

// for every key of local map, I would like to create a
// 'minimap' that is just that one key and its associated
// <set> values.
for (map<CString, set<CString> >::iterator i = local_map.begin(); i != local_map.end(); i++) {

// The memory for *pm will be deleted on the receiving end.
map<CString, set<CString> > *pm = new map<CString, set<CString> >;
pm->insert(pair<CString, set<CString> >(i->first, i->second));
PostMessage(WM_SENDMAP, (WPARAM)pm, NULL);
}
// local_map should deallocate itself right here yes?
}

void OnMessageFunctionReceiveMap(WPARAM w, LPARAM l)
{
// This is the minimap posted from function1.
map<CString, set<CString> > *pReceivedMap = (map<CString, set<CString> > *)w;

// Now I would like to set a member variable of
// a thread object equal to the received mini map.
m_pThreadObject->SetMap(pReceivedMap);

// Now the memory allocated for this minimap in function1
// should be taken care of?
delete pReceivedMap;
}

void CThreadObject::SetMap(map<CString, set<CString> > *map)
{
// I would like m_MemberLocalMap to be filled with the
// values from *map, which it does, but does this
// mean that m_MemberLocalMap will incorrectly still
// be retaining any allocated memory from *map?
m_MemberLocalMap = *map;
}


Again, I don't receive any memory leak warnings, but everytime I run the app I keep getting an extra 20k in memory usage. I have tested all this out before adding the <map>, with no problems so I'm pretty sure it has something to do with this.

Thanks for any info!

 

 

 


Reply

osknockout
why don't you just do a new()...delete()
or if you're in C, freelloc, etc. around the
whole thing?

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : memory, scope

  1. Accessing Physical Memory
    Important needed for project (3)
  2. Dynamic Memory Allocation
    (2)
    I'm writing a didactic software that can store an undefined number of int in a dynamic list. I
    wrote a routine too that can delete an int and point the previous node to the next node after the
    one I've deleted. But I've a problem: If I try to delete a number that isn't in the
    list, software crashes! Why? If the routine doesn't find the number it should simply do
    nothing CODE #include <stdio.h> #include <stdlib.h> void crealista();
    void visualizzalista(); void eliminanumero(); struct elenco {   int ....
  3. Variable Scope
    (3)
    Below is my code, which pretty much follows the Ivor Horton book on learning C++. My question is
    this: Why does the program return the value of "80" for count3 in the last cout statement? Why
    doesn't it return a value of "50", since the cout statement is located in the outer block and so
    should reference the value made in the initializiation made near the top of main(). What am I not
    understanding here? Thanks. CODE //ex2_06.cpp //demonstrating variable scope
    #include<iostream> using namespace std; int count1 = 100; //Global version fo count1 int
    m....
  4. Memory Share
    (0)
    Hey i looked into this and saw there were two methods: memory mapping and data_seg(built in memory
    mapping) however, being the noobie i am i can't seem to get this to work - all i want to do is
    share a few integers and bools across multiple(in this case - two) Dll's (these are injected
    into an exe) i.e 'int test' is set to '3' in first.dll -- and subsequently in
    second.dll 'test' is also set on to '3', and then i can perform if statements while
    the two values are the same over the 2 dll's....

    1. Looking for memory, scope

Searching Video's for memory, scope
advertisement



Free Memory After Out Of Scope?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE