Menu

What does an "Access Violation &q...

2010-01-17
2012-09-26
  • Joe Creaney

    Joe Creaney - 2010-01-17

    I am trying to write a win32 bitmap object. The algorithm works fine in a
    function but when I create an object out of the same code I get bugs. When I
    debug the program I get an access violation (segmentation Fault).

     
  • Joe Creaney

    Joe Creaney - 2010-01-17

    What does access violation mean?

     
  • cpns

    cpns - 2010-01-18

    It means that the process has attempted to access a memory location that is
    invalid. It may be causes by actions such as dereferencing an invalid pointer,
    or overflowing the process stack, or overrunning an array bound.

     
  • Joe Creaney

    Joe Creaney - 2010-01-18

    Is there a way to post code or to find out why this is happening. I am trying
    to create a color bitmap object. The algorithm works as a function but not as
    an object. Where would I look to try to correct this? I have to use a pointer
    or a vector to hold the data for the bitmap.

     
  • cpns

    cpns - 2010-01-18

    s there a way to post code

    Paste it into the composer and mark with code tags (use the buttons or
    manually type BBCode) - that's if SourceForge does not spontaneously change
    its mark-up syntax again!

    or to find out why this is happening

    The best way is to use a debugger; but good-luck with Dev-C++'s debugger - it
    sucks. There are better free tools.

    The algorithm works as a function but not as an object

    You mean "as a class"? An object is an instance of a class, you cannot have an
    object without a class to instantiate.

    I have to use a pointer or a vector to hold the data for the bitmap.

    Why the imperative? Remember that a pointer is not itself a container, it has
    to point to something valid, and large enough.

    Clifford

     
  • Joe Creaney

    Joe Creaney - 2010-01-23

    What I mean is my algorithm works as a function but runs into problems when
    put the same information in an object. That is I put the same data is a
    class{}; and functions in that class.

    I have a constructor where I:
    for(int lp = 0; lp != size; lp++){
    if(num == 16){num =0;}
    bitData.push_back(0);
    }

    I display like this:
    dc = CreateCompatibleDC(NULL);

    BYTE * bits = flip();

    void *pvBits;

    hbitmap= CreateDIBSection(dc,&bmi,
    DIB_RGB_COLORS, &pvBits,NULL,0 );

    CopyMemory(pvBits, bits, 16*16);

    SelectObject(dc, hbitmap);

    StretchBlt(hdc, ac, dw,165,165,dc,0,0,16,16,MERGECOPY);

    Then I use this function:

    This is my header.
    class bitmap{

    HBITMAP hbitmap;
    BITMAPINFO bmi;

    HDC dc;

    std::vector<BYTE>bitData;

    BYTE * Bits;

    BYTE size;
    int acc;
    int dwn;

    void create();
    BYTE* flip();

    public: ...

    I wonder why the pointers are not pointing to anything is there a problem of
    scope, I have the constructor go out of scope and the data that the pointer
    points to go away leaving problems that I might be having?

     
  • cpns

    cpns - 2010-01-23

    So when I explain how to post code, you choose to ignore the advice!? Good
    start.

    Posting fragments may not help, without seeing how those fargments fit into
    your code, all information about scope, sequence, and initialisation is lost.
    For example, how do I know that size has a valid value in the constructor.
    You have not even shown how this class is instantiated (i.e. how the object is
    created). Neither have you explained the nature of the failure.

    One thing I note is that you have a class variable Bits _and a local variable
    _bits
    . Was that intentional? It is certainly not best coding practice if it
    was.

    Your approach to describing your problem is not helping you; a better approach
    is to use a debugger, step through the code, and fix it yourself. However I
    cannot recommend Dev-C++'s integarted debugger at all!

     
  • Joe Creaney

    Joe Creaney - 2010-01-23

    Thanks for the advice. I am not sure what you mean in posting code.

    I try not to post too much code because it gets too long. I often find it
    difficult to post code of problems I have in complex problems. Basically I
    need to create an array of 255 BYTES or use a vector. I push back 255 0s in
    the vector or I can loop in 255 0 into any array. Which that array has to be a
    pointer in an object. I can't declare the size in the header of the object and
    I might want a different size later and I will have to use a pointer to deal
    with that.

    Once I get the basics of the bit map working I will add on more and more
    functions be able to edit that array of data and the colors. I also plan to
    create an editor as I had done with a monochrome bitmap. I am trying to put
    data in an array for a graphic I have done everything I am having problems
    with past objects and the algorithm works in a function. I always find it
    difficult to post pages of code.

     
  • cpns

    cpns - 2010-01-23

    I am not sure what you mean in posting code.

    Right above where you type your message in this forum is a row of buttons, did
    curiosity never cause you to investigate those (or the "BBCode" linke below
    the message box)? HTML rendering reduces multiple whitespacce to a single
    space, so all code indenting is removed. Your three posted code fragments
    should have looked like:

      for(int lp = 0; lp != size; lp++){
        if(num == 16){num =0;}
        bitData.push_back(0); 
         }
    
      dc = CreateCompatibleDC(NULL);
    
      BYTE * bits = flip();
    
      void *pvBits;
    
      hbitmap= CreateDIBSection(dc,&bmi,
                              DIB_RGB_COLORS, &pvBits,NULL,0 );
    
      CopyMemory(pvBits, bits, 16*16);
    
      SelectObject(dc, hbitmap);
    
      StretchBlt(hdc, ac, dw,16*5,16*5,dc,0,0,16,16,MERGECOPY);
    
    class bitmap{
    
      HBITMAP hbitmap;
      BITMAPINFO bmi;
    
      HDC dc;
    
      std::vector<BYTE>bitData;
    
      BYTE * Bits;
    
      BYTE size;
      int acc;
      int dwn;
    
      void create();
      BYTE* flip();
    
    public:  ...
    

    Though it is probably academic, this is not the best formatted code I've seen
    in any event.

    I try not to post too much code because it gets too long.

    That is sometimes a good idea, but if it is not enough to reproduce your
    problem, it is probably not enough to solve your problem. It is best to post
    something that is stand-alone compilable unless you know precisely where the
    problem lies, and judging from the fragments you posted, you have no idea. You
    do not need to worry about length if what you post is all useful and relevant
    information, randomly selecting broken fragments is far worse; like asking me
    why your car does not work by showing me the steering wheel, the tail light,
    and a bolt from somewhere unknown under the hood!

    If you do not wish to post complete code, you should at least post complete
    functions and a complete class definition, and the functions you post should
    include those that are failing. And you should post at the very least an
    example of how you are using this class in a way that causes the failure. For
    example if you know that the constructor is not correct, post the complete
    constructor and an example of the object instantiation. For example:

    class  cMyClass
    {
        // complete definition here
    } ;
    
    cMyClass::cMyClass()
    {
        // complete constructor here
    }
    
    // other complete member functions needed to reproduce the error here
    
    int main()
    {
        cMyClass  object ;
        // Show or explain how object is invalid here
    }
    

    Your original statement "works fine in a function but when I create an object
    out of the same code I get bugs"
    makes little sense since a function and a
    class are not analogous, there is no direct mapping between a single function
    and an equivalent class; to think there might be would be to misunderstand the
    object oriented paradigm completely. Without seeing either your original
    implementation or the class implementation, the statment is merely a
    platitude. The problem is not that classes do not work, but rather that you
    wrote code that does not work; how can we tell why without seeing the code?

     
  • cpns

    cpns - 2010-01-23

    Damn Sourceforge and their constant mark-up tinkering! Now code tagged text is
    rendered in a proprotional font! Why does anyone trust these idiots to host
    their projects!? Just when they seem to have debugged the elagentaly simple
    Mark-Down compositor, they replace it with the ancient and cumbersome BBCode
    and screw it up at the same time.

    Maybe they thonk it does not matter because no one uses it; maybe no one uses
    it because it sucks.

     
  • Joe Creaney

    Joe Creaney - 2010-01-24

    Thanks all for the help. I will see if I can find the problems. I have worked
    with these issues in the past and I have not had problems, I am trying to find
    out how what I am doing is different than I had done in previous projects and
    what I got to work in a function but has bugs in an object.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.