C/C++ Help
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Remember me

Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
Unread Dev Articles Community Forums Sponsor:
Old March 24th, 2009, 05:54 PM
RnR RnR is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
Join Date: Mar 2009
Posts: 1 RnR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 8 sec
Reputation Power: 0
Memory & arrays - "Free Heap block modified after it was freed" Error..please help

Hello everyone,

I'm having problems while trying to work with dynamic memory.

I'm writing a project under win32 and compiling with VS2008.
Basically I have this button that whenever the user is pressing it I have to add another object to a list of objects of the same type. This object is a class which has a constructor that goes like this: Obj(char a, char b)
So what I am doing is keeping two arrays of pointers to pointers of such object and temporary pointer for such object.
Defined in the global scope as:

Obj ** List; // holds the list of objects have been declared so far
Obj ** T_List; // hold the list of object for temp use when I want to add new one
Obj * Tmp_Obj; // define the new object before it is added to the list

Also I keep a counter to keep track of how much objects have been declared so far:

Int Obj_c=0;

Now in the beginning of my WinMain I have:

List = new Obj * [Obj_c];

After that comes the main loop.

Now, whenever a button is pressed I get a message to its WndProc and I start the following flow to add a new object to the list:

1 : Tmp_Obj = new Obj (val1,val2);
2 : T_List = new Obj * [Obj_c];
3 : For(int i=0; i<Obj_c;i++) { T_List[i]=List[i];}
4 : Delete [] List;
5 : Obj_c++;
6 : List = new Obj * [Obj_c];
7 : For(int i=0; i<Obj_c-1;i++) { List[i]=T_List[i];}
8 : List[Obj_c-1]=Tmp_Obj;
9 : Delete [] T_List;

Obj Class do not hold any pointers so no new operator is in the constructor.
I know I can declare new[0] so that is a valid statement.

Problem is whenever I press the button, the program is crushing with the following error:

HEAP: Free Heap block 3963d8 modified at 396400 after it was freed
Windows has triggered a breakpoint in proj.exe

When I run in debug mode and I go line by line the program crushes when it gets line number 2 (meaning the line 1 was ok and when it tries to execute line 2 it crushes). I get the options "break" and "continue" and if I press continue it seems like everything is working well (but sometimes it crushes ,after the first continue, on the lines in the main loop that says :

until I press the button again and than I get the same error again but with different addresses.
Also when the program stops, it is stopped at the file new.cpp (the VS2008 library file I suppose) at the function:

void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
{ // try to allocate size bytes
void *p;
while ((p = malloc(size)) == 0)
if (_callnewh(size) == 0)
{ // report no memory
static const std::bad_alloc nomem;

return (p);

On the line that says: while((p = malloc .

I have no idea what I'm doing wrong, I think the flow of adding new object to the list is legit, but hey, VS thinks differently
Can someone please help me with this? What could be the error? Or at least how can I debug it better?

Thanks to whomever can help.

Btw, no use of pasting the entire code because it's too long, I only using the dynamic memory where I showed you here.

Reply With Quote
Old March 25th, 2009, 07:25 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 13
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
The break happens at the line that contains malloc because that's where the heap is checked. The actual erronous write happens before that - as I believe you're aware of.

I'd have you look through your code for anywhere you store a copy of any of the pointers from List, or a copy of the List itself, such that you may be accessing them at their old position after they have been moved from List to T_List.

Using new/delete you're not strictly C, so therefore I'll give the following advice. Replace the Obj ** List; with vector<Obj*> List; and it'll handle all that lowlevel stuff for you - less chance for you to mess up, and you're very unlikely to make your code better. See http://www.cppreference.com/wiki/stl/vector/start for more information on how to use it. Of particular interest is push_back() (add a new element) and I guess maybe erase() (to remove a specific element). You can use List.at(x) rather than List[x] if you want range checking.

You'll have to
#include <vector>
and then either
using std::vector;
using namespace std
or prefix each use with std::

Reply With Quote

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - "Free Heap block modified after it was freed" Error..please help

Developer Shed Advertisers and Affiliates

Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap