Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using new causes many memory problems

Status
Not open for further replies.

Korach

Programmer
Jun 2, 2003
303
IL
Hi,

I made a class for image processing. I use new operator a lot, and I have many memory exceptions. The new always fails in some point of the program, and Every time I change something in the code (with no concern to the new calls), the new fails in other new call. I think it is because all allcation are in the local heap. does someone know to solve it?
 
Are you using the Multithreaded runtime library if you have a multithread program? Otherwise your program is NOT thread-safe. And memory allocation calls can certainly mess up if not properly serialized. (The multithread runtime library handles serialization for you.)

I REALLY hope that helps.
Will
 
>I think it is because all allcation are in the local heap. does someone know to solve it

Well, can you put the stuff on the stack?

Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
I use the CFile library because the standard fread failed to read large files. I had to use the Multithreaded runtime library, because otherwise the linking fails. I used the Multithreaded runtime library through the option /MTd in the project options.
If put the stuff on the stuck will help, how can I make the new operator do it?

Thanks
 
Since your're writing a class to do image processing it stands to reason youre working with large amounts of memory. Does the machine youre working on have the pysical memory? I once wrote a program to stress a machine by eating memory. In order to be effective it had to eat pysical memory. I alloacted it in one megabyte segments and continuously accessed them to prevent storage on disk. When the target amount of memory was set to more than the computer pysically had, the program invarably crashed. However when it was modified to either drop the continous access of data, or not access it in such a tight loop it worked fine.

WR
 
Yes, I use a large memory, but my computer has 512MB RAM. My program worked fine because of a line like that:
myData = new picdata[label];
I changed this line to:
myData = new picdata[label*2];
even I didn't need the "*2" at all.
An allocation that took place before this allocation always failed until I added the "*2". When I re-arranged my class functions I just replaced the place of the functions, not the runtime order, and the allocation failed again. Since then my program failed evrey day on different allocation. This sounds strange, but this is what happened.
Even when my program worked fine, it didn't eat half of my RAM according to the task manager.

Thanks
 
Sounds like you might be corrupting memory. One of the indicators of a memory corruption bug is when you rearrange the memory of the application the error behavior changes.

One of the most common ways memory corruption occurs is using a pointer type variable that has not been properly initialized.


-pete
 
Maybe you right, because I sometimes receiv an error that says "DAMAGE... block..."
I initialized all the pointers properly. I used an int pointers and an unsigned char pointers. Some of the pointers represent a 2-D and 3-D matrices.
Somtimes the failure was on delete statement, and sometimes it happened on new statement when I tried to allocate the head of a 3-D 10*10*3 unsigned char matrix, and sometimes when I tried to allocate a another matrix 10*10*3, and it always failed on the [2][4] index.
I think I need a different approach to use the new operator.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top