Thread: n00b needs help with api graphics acting very weird

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    2

    Question n00b needs help with api graphics acting very weird

    Hi everyone!
    This is my first post here.

    I am currently working on writing an image/sprite editor for the win32 api. it is supposed to handle my own file format, and is actually a port of an intel x86asm program i wrote about a year ago.
    I am quite new at windows programming, but all seemed to work fine and well (thanks to TheForger and Charles Petzold), until some time ago when i started to work with actually drawing the images, with the palette(it is mostly supposed to handle 256 colours, will add support for 24bit colour later, though.)
    in one mdichild, and the frames in others.

    At first glance the image/palette output routine seemed to work ok, but sooner or later it gets all FUBAR and you have to close the program. this was were frustrating, and i tried all kinds of ways to solve the problem. anyway, now i am posting on these message boards, hoping that some kind soul with a better knowledge of the workings of the windows api might have a clue of what i am doing wrong.

    screens:
    here is a picture of the the very basic interface atm, before:
    http://trotskij.mine.nu/before.jpg
    and after:
    http://trotskij.mine.nu/after.jpg (more or less, differs a bit.)

    here is a zip with a stripped down version of the program (mostly just the gui-stuff, most other headers arent included, those who handles the pictures, mem management etc) that prints a random palette and a static picture (some rectangles) in mdichildren.
    http://trotskij.mine.nu/Project.zip

    using this you can recreate the problem.
    (just choose colours in the palette, minimize the windows, move them, minimize the program, you cant really do much, as i stripped most of the things that do stuff.)
    sooner or later it will crash..
    i have no idea what is causing the problem. probably i am using WM_PAINT wrong or something. as you can see most of the image output code is very ugly, i dont use custom devicecontexts or blitting or stuff.. mostly just rectangles, taking into account the zoomlevel and gridwidth, if any. the routines are far from complete, but it is very hard to work on them when it continously crashes.
    i would be *very* grateful for any ideas on how i could write it better.

    i run winXP on a 400mhz celeron, 256MB ram.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Your error is in the function used to create the brushes

    SelectObject (hdc, CreateSolidBrush (temppal[colour]));

    Each call you select into the HDC a new brush. Next time a brush is selected into the HDC this one is lost == GDI memory leak.

    A HDC must be returned to its original condition before it can be released / deleted and all GDI drawing objects must be removed from the hdc before they can be released / deleted. The exception to this is stock objects (which must be removed but do not have to be deleted).

    Code:
    //create (will need the cast if using .cpp files as opposed to .c files)
    hOldBrush=(HBRUSH)SelectObject (hdc, CreateSolidBrush (temppal[colour]));
    
    // use
    
    //delete
    hCreatedBrush=(HBRUSH)SelectObject (hdc, hOldBrush);
    DeleteObject(hCreatedBrush);
    //hdc now able to be released by EndPaint()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    2
    Now it works perfectly

    thanks a lot!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3d graphics without API
    By h3ro in forum Game Programming
    Replies: 6
    Last Post: 05-31-2008, 11:51 AM
  2. Which graphics API?
    By homeyg in forum Game Programming
    Replies: 6
    Last Post: 03-27-2005, 05:05 PM
  3. BGI Graphics in Windows API
    By AtomRiot in forum Windows Programming
    Replies: 1
    Last Post: 07-29-2003, 05:32 PM
  4. Flickery API graphics
    By Magos in forum Windows Programming
    Replies: 7
    Last Post: 10-23-2002, 06:49 AM