LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-09-2012, 11:17 PM   #16
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32

Hi, thanks everyone who has helped out on this. The issue is resolved. Turns out to be indeed due to a bug in the code on handling memory allocations. The issue was with the following, where I have allocated memory without including the C String "\0" trailing character. For example, if the string is "I have", I was only doing "numOfFields = 6" rather than "numOfFields = 7".
Code:
int * storage = (int*) malloc(numOfFields * sizeof(int));
Also, while using Valgrind, it was reporting that the code is leaking memory. When I added some "free()" it introduced another bug. The reason is better explained with the following psuedo code.
Code:
std::map<std::string, int *>map_ObjName_PointerAddress;
std::map<std::string, int *>map_ObjName_FieldArrayAddress;

void retrieveObjectsNamesAndPointers() {
	int * address;
	// Some code to assign address;
	
	// Do something here to insert mapping between "object names" and "pointers to these objects" to map_ObjName_PointerAddress.
	map_ObjName_PointerAddress.insert(std::pair<std::string, int *>("ObjName1", address));
}

void doSomethingWithObject(ObjectType * pObject) {
	// Call method lookUpFields() to retrieve some details about the objects before doing something with them.
	lookUpFields(pObject);

	// Routine to do something here based on the fields retrieved for the objects.
}


void lookUpFields(ObjectType * pObject) {
	int * fieldsArray = (int*) malloc(numOfFields * sizeof(int));
	fieldsArray = CObject::map_ObjName_PointerAddress.find(pObject->getName)->second;

	// Insert the new object name to fields array address map to map_ObjName_FieldArrayAddress
	map_ObjName_FieldArrayAddress.insert(std::pair<std::string, int *>(pObject->getName, fieldsArray));

	// Can not do following upon exit:
	// free(fieldsArray);
	// fieldsArray = 0;
}
The code will invoke the following functions in sequence.
retrieveObjectsNamesAndPointers()
doSomethingWithObject()

Therefore, following sequence of events occur:
1) Memory allocated in the function retrieveObjectsNamesAndPointers().
2) Pointer to this memory is inserted into map_ObjName_PointerAddress.
3) Pointer to this memory, as stored in map_ObjName_PointerAddress, is retrieved in lookUpFields(). This memory address retrieved is then assigned to the local pointer fieldsArray.
4) When the "free(fieldsArray)" is called (highlighted in red and commented out here), it actually frees the memory originally allocated in step 1. Therefore, when we exits lookUpFields() and returns to doSomethingWithObject(), the items stored in the memory location is no longer available, causing problems.

Also the following line is not serving any purpose.
Code:
int * fieldsArray = (int*) malloc(numOfFields * sizeof(int));
This is because the following line, is not copying the entire block of memory to the new memory location. We're merely allocating memory on the heap (so when lookUpFields() exits this fieldsArray is still there which is also why Valgrind is complaining about leaking memory), and setting the first memory location of fieldsArray to point to the memory location assigned in Step 1.
Code:
fieldsArray = CObject::map_ObjName_PointerAddress.find(pObject->getName)->second;
In the end, I simply removed the lines highlighted in both red and blue. I simply do the following instead.
Code:
map_ObjName_FieldArrayAddress.insert(std::pair<std::string, int *>(pObject->getName, CObject::map_ObjName_PointerAddress.find(pObject->getName)->second));
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leaks.. *** glibc detected *** ./SuffixTree: malloc(): memory corruption: 0x00 evansash Programming 12 03-21-2011 01:17 PM
*** glibc detected ***: malloc() memory corruption Dinarchik Programming 5 02-16-2010 05:38 PM
*** glibc detected *** ruby: malloc(): memory corruption: priceey Linux - Software 0 10-16-2009 05:24 PM
*** glibc detected *** malloc(): memory corruption arvind.ayyangar Programming 2 11-20-2006 11:59 PM
glibc detected *** malloc(): memory corruption: abefroman Linux - Software 2 04-12-2006 12:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration