Jump to content

Can heap get corrupted without dynamic memory allocation?

- - - - -

  • Please log in to reply
10 replies to this topic

#1
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
Hello,

the title almost says it all. I have a program which consists of thousands of lines. Recently I started
getting messages that the heap is corrupted. Visual Studio 2008 is the messenger of doom delivering
the news. As I understand, the problem could be almost anywhere but if we presume for the moment
that the DLLs aren't the problem, is it possible to corrupt heap with only a few function calls and without
any dynamic memory allocation?

---------- Post added at 02:31 PM ---------- Previous post was at 02:20 PM ----------

And in addition, if anyone knows what the following is symptomatic of, I'll declare you my personal saviour :)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    const int size = 2 << 15;
    int* mem = new int(size);
    memset(mem, 0, size); // Crash executing this statement!
    return 0;
}
With value 15, it'll result in crash with notification: "Unhandled exception at 0x01652aad in myprog.exe: 0xC0000005: Access violation writing location 0x003c0000."
With 14, it'll crash later on and for other reasons (it appears).

Is this something that would be the result of heap corruption?
I personally have never faced heap corruption so I'm not sure ..

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I think you meant to use square brackets, to create an array, right? What you're doing is creating a pointer with value of 2 << 15.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

Flying Dutchman said:

I think you meant to use square brackets, to create an array, right?
I think you're right :D

In any case, that was just a try and not the real problem. The question still remains.

#4
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
This has been solved (sort of), but I still don't understand why the following causes the crash (possibly heap corruption).

There's the files header.h and impl.cpp and they contain (skipping irrelevant and simplifying a lot)
namespace vehicles {
    class ICar;
}

using vehicles::ICar;

void calculateWorth(shared_ptr<ICar> car);

#include <header.h>
#include <vehicles.h>

using namespace vehicles;

void calculateWorth(shared_ptr<ICar> car)
{
    // Does something ..
}

There a few concrete classes (OldCar, NewCar etc) and the ICar is an abstract interface class (contains only pure virtual methods).
The cause for the heap corruption was the forward declaration in the "header.h"-file.
When I just use include, no problem.

Why can't I forward declare an abstract class?

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I have a feeling you've simplified away the real problem. There's nothing in your code that allocates or deallocates anything from the heap.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

WingedPanther said:

I have a feeling you've simplified away the real problem. There's nothing in your code that allocates or deallocates anything from the heap.

I've been testing my problem further. I didn't understand the issue because, as you said, there's no action in the heap. I thought it was heap corruption merely because Visual Studio 2008 claimed it was. Now that I've tested the problem in its simple form with G++, there is no problem. Everything works as it should. I'm starting to think this an issue with VC++ ..

#7
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 551 posts
  • Location:/etc/passwd
Switch to MinGW and Code::Blocks away with MS and towards freedom!!
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#8
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

bbqroast said:

Switch to MinGW and Code::Blocks away with MS and towards freedom!!

Nice thought (thou not possible in this case) ..

#9
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

denarced said:

Nice thought (thou not possible in this case) ..

Why not possible?

#10
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

RhetoricalRuvim said:

Why not possible?

The whole project is pretty much tied to Visual Studio. A lot of code and while the idea of avoiding proprietary stuff is appealing (and especially Microsoft stuff), no one is going to support spending the hours necessary to get rid of the dependencies.

#11
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
The Windows API could be doing dynamic memory allocation in the background. Just my two cents.
Programming is a journey, not a destination.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users