"delete" confusion

Started by
8 comments, last by Will F 18 years, 7 months ago
I have writin the base of a program that works very well currently but when ever I put a delete anywhere in the main program the whole thing shuts down and I get a multiple deletion report form my memory logger like this (after the first few lines you get the idea)

                    MEMORY        TICKS
deallocating for |  18553680  at  89675266
deallocating for |  18558304  at  89675267
deallocating for |  18562928  at  89675268
deallocating for |  18567552  at  89675268
deallocating for |  16932352  at  89675269
deallocating for |  17976816  at  89675270
deallocating for |  17825800  at  89675271
deallocating for |  16781320  at  89675272
deallocating for |  17230616  at  89675273
deallocating for |  17231136  at  89675274
deallocating for |  18275080  at  89675275
deallocating for |  18275600  at  89675276
deallocating for |  16493168  at  89675277
deallocating for |  16493688  at  89675278
deallocating for |  17529432  at  89675279
deallocating for |  17529952  at  89675280
deallocating for |  17526792  at  89675281
deallocating for |  17527312  at  89675282
deallocating for |  16482312  at  89675283
deallocating for |  16482832  at  89675284
deallocating for |  18272264  at  89675286
deallocating for |  18272784  at  89675287
deallocating for |  18273304  at  89675288
deallocating for |  17227784  at  89675289
deallocating for |  17228304  at  89675290
deallocating for |  17228824  at  89675292
deallocating for |  16783224  at  89675293
deallocating for |  16783744  at  89675294
deallocating for |  16784264  at  89675296
deallocating for |  16784784  at  89675296
deallocating for |  17827688  at  89675297
deallocating for |  17828208  at  89675298
deallocating for |  17828728  at  89675300
deallocating for |  17829248  at  89675301
deallocating for |  17973256  at  89675302
deallocating for |  17973776  at  89675304
deallocating for |  17974296  at  89675305
deallocating for |  17974816  at  89675307
deallocating for |  16928776  at  89675308
deallocating for |  16929296  at  89675310
deallocating for |  16929816  at  89675311
deallocating for |  16930336  at  89675313
deallocating for |  18550792  at  89675314
deallocating for |  18551312  at  89675316
deallocating for |  18551832  at  89675318
deallocating for |  18552352  at  89675319
deallocating for |  18552872  at  89675321
deallocating for |  17081488  at  89675322
deallocating for |  17082008  at  89675324
deallocating for |  17082528  at  89675326
deallocating for |  17083048  at  89675327
deallocating for |  17083568  at  89675329
deallocating for |  18125952  at  89675331
deallocating for |  18126472  at  89675333
deallocating for |  18126992  at  89675335
deallocating for |  18127512  at  89675336
deallocating for |  18128032  at  89675337
deallocating for |  17380304  at  89675339
deallocating for |  17380824  at  89675341
deallocating for |  17381344  at  89675343
deallocating for |  17381864  at  89675345
deallocating for |  17382384  at  89675346
deallocating for |  17382904  at  89675348
deallocating for |  17674248  at  89675354
deallocating for |  17674768  at  89675356
deallocating for |  17675288  at  89675358
deallocating for |  17675808  at  89675360
deallocating for |  17676328  at  89675362
deallocating for |  17676848  at  89675364
deallocating for |  16629768  at  89675367
deallocating for |  16630288  at  89675369
deallocating for |  16630808  at  89675379
deallocating for |  16631328  at  89675380
deallocating for |  16631848  at  89675382
deallocating for |  16632368  at  89675385
deallocating for |  16479472  at  89675387
deallocating for |  16479992  at  89675389
deallocating for |  16480512  at  89675398
deallocating for |  16481032  at  89675400
deallocating for |  16481552  at  89675403
deallocating for |  18572176  at  89675405
deallocating for |  18572696  at  89675407
deallocating for |  18573216  at  89675409
deallocating for |  18573736  at  89675416
deallocating for |  18574256  at  89675418
deallocating for |  18574776  at  89675420
deallocating for |  18419720  at  89675422
deallocating for |  18420240  at  89675424
deallocating for |  18420760  at  89675426
deallocating for |  18421280  at  89675428
deallocating for |  18421800  at  89675432
deallocating for |  18422320  at  89675434
deallocating for |  4059552  at  89675437
deallocating for |  4060072  at  89675439
deallocating for |  4060592  at  89675442
deallocating for |  4061112  at  89675445
deallocating for |  4061632  at  89675448
deallocating for |  4062152  at  89675453
deallocating for |  4062672  at  89675456
deallocating for |  16330976  at  89675459
deallocating for |  16331496  at  89675462
deallocating for |  16332016  at  89675464
deallocating for |  16332536  at  89675467
deallocating for |  16333056  at  89675470
deallocating for |  16333576  at  89675473
deallocating for |  16334096  at  89675476
deallocating for |  18424208  at  89675480
deallocating for |  18424728  at  89675483
deallocating for |  18425248  at  89675486
deallocating for |  18425768  at  89675489
deallocating for |  18426288  at  89675492
deallocating for |  18426808  at  89675495
deallocating for |  18427328  at  89675498
deallocating for |  17678560  at  89675501
deallocating for |  17679080  at  89675504
deallocating for |  17679600  at  89675507
deallocating for |  17680120  at  89675510
deallocating for |  17680640  at  89675514
deallocating for |  17681160  at  89675518
deallocating for |  17681680  at  89675521
deallocating for |  16691552  at  89675524
deallocating for |  17375240  at  89675527
deallocating for |  16700280  at  89675530
deallocating for |  17375760  at  89675533
deallocating for |  16709008  at  89675536
deallocating for |  17376280  at  89675539
deallocating for |  16717736  at  89675543
deallocating for |  17376800  at  89675546
deallocating for |  16726464  at  89675548
deallocating for |  17377320  at  89675552
deallocating for |  16735192  at  89675555
deallocating for |  17377840  at  89675558
deallocating for |  16743920  at  89675561
deallocating for |  17378360  at  89675564
deallocating for |  17301576  at  89675567
deallocating for |  17302096  at  89675570
deallocating for |  17302616  at  89675574
deallocating for |  17303136  at  89675577
deallocating for |  17303656  at  89675580
deallocating for |  17304176  at  89675583
deallocating for |  17304696  at  89675586
deallocating for |  16777272  at  89675588
deallocating for |  16777792  at  89675591
deallocating for |  16778312  at  89675594
deallocating for |  16778832  at  89675598
deallocating for |  16779352  at  89675601
deallocating for |  16779872  at  89675604
deallocating for |  16780392  at  89675607
deallocating for |  18120712  at  89675610
deallocating for |  18121232  at  89675614
deallocating for |  18121752  at  89675617
deallocating for |  18122272  at  89675620
deallocating for |  18122792  at  89675623
deallocating for |  18123312  at  89675626
deallocating for |  18123832  at  89675629
deallocating for |  16846864  at  89675632
deallocating for |  16851488  at  89675635
deallocating for |  16856112  at  89675638
deallocating for |  16860736  at  89675641
deallocating for |  16865360  at  89675644
deallocating for |  16869984  at  89675647
deallocating for |  16874608  at  89675650
deallocating for |  16879232  at  89675653
deallocating for |  16883856  at  89675657
deallocating for |  16888480  at  89675660
deallocating for |  16893104  at  89675663
deallocating for |  16897728  at  89675666
deallocating for |  16902352  at  89675669
deallocating for |  16906976  at  89675671
deallocating for |  16911600  at  89675675
deallocating for |  16916224  at  89675678
deallocating for |  16920848  at  89675681
deallocating for |  16925472  at  89675684
deallocating for |  16925992  at  89675687
deallocating for |  16926512  at  89675690
deallocating for |  16927032  at  89675694
deallocating for |  16927552  at  89675697
deallocating for |  16928072  at  89675700
deallocating for |  16957496  at  89675703
deallocating for |  16962120  at  89675706
deallocating for |  16966744  at  89675709
deallocating for |  16971368  at  89675712
deallocating for |  16975992  at  89675715
deallocating for |  16980616  at  89675719
deallocating for |  16985240  at  89675722
deallocating for |  16989864  at  89675725
deallocating for |  16994488  at  89675728
deallocating for |  16999112  at  89675732
deallocating for |  17003736  at  89675735
deallocating for |  17008360  at  89675738
deallocating for |  17012984  at  89675741
deallocating for |  17017608  at  89675744
deallocating for |  17022232  at  89675748
deallocating for |  17026856  at  89675751
deallocating for |  17031480  at  89675754
deallocating for |  17036104  at  89675756
deallocating for |  17040728  at  89675759
deallocating for |  17045352  at  89675762
deallocating for |  17049976  at  89675766
deallocating for |  17054600  at  89675769
deallocating for |  17059224  at  89675772
deallocating for |  17063848  at  89675775
deallocating for |  17068472  at  89675778
deallocating for |  17073096  at  89675781
deallocating for |  17077720  at  89675784
deallocating for |  17078240  at  89675788
deallocating for |  17078760  at  89675791
deallocating for |  17079280  at  89675794
deallocating for |  17079800  at  89675796
deallocating for |  17104608  at  89675799
deallocating for |  17109232  at  89675802
deallocating for |  17113856  at  89675805
deallocating for |  17118480  at  89675809
deallocating for |  17123104  at  89675812
deallocating for |  17127728  at  89675815
deallocating for |  17132352  at  89675818
deallocating for |  17136976  at  89675821
deallocating for |  17141600  at  89675824
deallocating for |  17146224  at  89675827
deallocating for |  17150848  at  89675830
deallocating for |  17155472  at  89675834
deallocating for |  17160096  at  89675837
deallocating for |  17164720  at  89675839
deallocating for |  17169344  at  89675842
deallocating for |  17173968  at  89675845
deallocating for |  17178592  at  89675848
deallocating for |  17183216  at  89675852
deallocating for |  17187840  at  89675855
deallocating for |  17192464  at  89675858
deallocating for |  17197088  at  89675861
deallocating for |  17201712  at  89675864
deallocating for |  17206336  at  89675867
deallocating for |  17210960  at  89675871
deallocating for |  17215584  at  89675874
deallocating for |  17220208  at  89675877
deallocating for |  17224832  at  89675879
deallocating for |  17225352  at  89675883
deallocating for |  17225872  at  89675886
deallocating for |  17226392  at  89675889
deallocating for |  17226912  at  89675892
deallocating for |  17252176  at  89675895
deallocating for |  17256800  at  89675898
deallocating for |  17261424  at  89675901
deallocating for |  17266048  at  89675905
deallocating for |  17270672  at  89675908
deallocating for |  17275296  at  89675911
deallocating for |  17279920  at  89675914
deallocating for |  17284544  at  89675917
deallocating for |  17289168  at  89675920
deallocating for |  17293792  at  89675922
deallocating for |  17298416  at  89675926
deallocating for |  17298936  at  89675929
deallocating for |  17299456  at  89675932
deallocating for |  17299976  at  89675935
deallocating for |  17300496  at  89675938
deallocating for |  17325736  at  89675941
deallocating for |  17330360  at  89675945
deallocating for |  17334984  at  89675948
deallocating for |  17339608  at  89675951
deallocating for |  17344232  at  89675954
deallocating for |  17348856  at  89675957
deallocating for |  17353480  at  89675960
deallocating for |  17358104  at  89675963
deallocating for |  17362728  at  89675966
deallocating for |  17367352  at  89675969
deallocating for |  17371976  at  89675972
deallocating for |  17372496  at  89675975
deallocating for |  17373016  at  89675978
deallocating for |  17373536  at  89675981
deallocating for |  17374056  at  89675985
deallocating for |  17374576  at  89675988
deallocating for |  17408056  at  89675991
deallocating for |  17412680  at  89675994
deallocating for |  17417304  at  89675997



seeing as how right now I am only deleting ONE THING I cant imagine what else it could be deleting. Also the Memory logger shows no allocations (it does usually) this is further testament to the fact that the program never even gets started. The section where my memory is dealocted is also only called once another thing is that my memory manager gives me bizzar deletion times, usually this only happens if I am doing somthing in memory before the program even starts. Heres my code

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <libiberty.h>
#include "SDL.h"
#include <SDL_TTF.h> 
#include "GFX.h"
#include "Entity.h"
#include "Unit.h"
#include "Map.h"
#include "Debugger.h"
#include "stdafx.h"
Debugger* Debug = new Debugger;
GFX* GFX_H;
void initialize()
{
    GFX_H = new GFX();
    GFX_H->Add_Map("TXT/Maps/Test_Map.txt", 2, 2);
    
};
static void
draw ()
{
    GFX_H->Update();

    SDL_Flip (GFX_H->screen);
    SDL_Delay (1);
}


int
main (int argc, char *argv[])
{
    char *msg;
    int done;
	initialize();

    /* Initialize SDL */
    if (SDL_Init (SDL_INIT_VIDEO) < 0)
    {
        asprintf (&msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
        MessageBox (0, msg, "Error", MB_ICONHAND);
        free (msg);
        exit (1);
    }
    atexit (SDL_Quit);

    /* Set 640x480 16-bits video mode */
    GFX_H->screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
    if (GFX_H->screen == NULL)
    {
        asprintf (&msg, "Couldn't set 640x480x16 video mode: %s\n",
          SDL_GetError ());
        MessageBox (0, msg, "Error", MB_ICONHAND);
        free (msg);
        exit (2);
    }
    SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);
    done = 0;
    while (!done)
    {
        SDL_Event event;
        /* Check for events */
        while (SDL_PollEvent (&event))
        {
            switch (event.type)
            {
            case SDL_KEYDOWN:
                break;
            case SDL_QUIT:
                done = 1;
                break;
            default:
                break;
            }
        }

        /* Draw to screen */
        draw ();
    };
    delete GFX_H;//error caused here, if I comment this out everything runs great
    GFX_H = 0;
    return 0;
}



thx for your help [grin]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
What does the constructor and destructor look like for the "GFX" class? Did you overload the "new" and "delete" operator?
Also, how are the times bizzare? Do they suddenly jump from the current measurement you have to the ones that are written in the log?
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Why are you calling free on a char * ? IIRC free is only to be used on malloc'd vars
asprintf is a Linux command which allocates memory to copy a string. As such, you need to free the string after use. My question is why are you using a Linux function on a Windows machine? Maybe you should be including unwindows.h?

http://linux.about.com/library/cmd/blcmdl3_asprintf.htm
http://www.cs.virginia.edu/~gfx/Courses/2004/RealTime/tools/

Uhh, might just be me, but why is there a semi-colon after the definition of the initialize function? Wouldn't that cause the compiler to bug up somewhat...
Quote:Original post by chad_420
Why are you calling free on a char * ? IIRC free is only to be used on malloc'd vars

Because asprintf allocates them. They should be freed using free. The semicolon is inappropriate (as is most of code to my taste) but it does not do any harm either. The deconstructor of GFX() would be most interesting I think.

Illco
Quote:Original post by Illco
Quote:Original post by chad_420
Why are you calling free on a char * ? IIRC free is only to be used on malloc'd vars

Because asprintf allocates them. They should be freed using free. The semicolon is inappropriate (as is most of code to my taste) but it does not do any harm either. The deconstructor of GFX() would be most interesting I think.

Illco


And here I was thinking that a semicolon placed after a function definition would cause a compiler error. :p

I think this is the code for gfx.h:
http://openmortal.sourceforge.net/doxygen/gfx_8h-source.html
Ok first of all may I just say that most of the stuff you guys commented on wasnt even code I typed, this:

intmain (int argc, char *argv[]){    char *msg;    int done;    /* Initialize SDL */    if (SDL_Init (SDL_INIT_VIDEO) < 0)    {        asprintf (&msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());        MessageBox (0, msg, "Error", MB_ICONHAND);        free (msg);        exit (1);    }    atexit (SDL_Quit);    /* Set 640x480 16-bits video mode */    GFX_H->screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);    if (GFX_H->screen == NULL)    {        asprintf (&msg, "Couldn't set 640x480x16 video mode: %s\n",          SDL_GetError ());        MessageBox (0, msg, "Error", MB_ICONHAND);        free (msg);        exit (2);    }    SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);    done = 0;    while (!done)    {        SDL_Event event;        /* Check for events */        while (SDL_PollEvent (&event))        {            switch (event.type)            {            case SDL_KEYDOWN:                break;            case SDL_QUIT:                done = 1;                break;            default:                break;            }        }        /* Draw to screen */        draw ();    }}


is just the defalut code put in there for a bare bones SDL application for Dev-C++.

Interestingly enough I didnt even know about the link posted about and GFX is a class I wrote myself. If you really want to know what it looks like...

#ifndef GFX_#define GFX_#include <SDL.h>#include "SDL_Image.h"#include <vector>#include <string>#include <fstream>class Collision_Handler;class Entity;class Map;class Tile;struct Collision_Relay;class GFX{      public:             int td, td2, Frame_Time;             GFX()              {                Sprites.resize(0);                td = 0;                td2 = 0;                Frame_Time = 0;                Off_SetX = 0;                Off_SetY = 0;                Lagging = false;             };             ~GFX()             {                Sprites.clear();                SDL_FreeSurface(screen);                screen = 0;                delete Curr_Map;                Curr_Map = 0;             };             SDL_Surface* screen;             std::vector<Entity*> Sprites;             bool Add_To(Entity* Add);             void Add_Map(std::string File_Name, int nHeight, int nWidth);             bool Update();               bool TempRectBlit(Entity* To_Blit);             bool AnimationBlit(Entity* To_Blit);             bool Off_Set_Blit(Entity* To_Blit);             bool Off_Set_Blit(Tile* To_Blit);             void Transfer_Blit(Entity* Transfer,SDL_Rect Animation_Rect);             float Get_Frame_Time();               int Time_Based(int move);             bool Lagging;             int Off_SetX, Off_SetY;             Map* Curr_Map;                    };#endif


there you go, (hunting season on my code)...

thx again

EDIT: almost forgot, the times are bizzar because it gives me like a hundred thousand ticks and im not running the program near that long. Yes I did override the new and delete operators
For the love of God! is a semi-colon after my function definitions really that noticable!? Its just a habit to put them their and they dont really do any harm so whats the big deal?
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Hi,

as far as I remember SDL, you should NOT call SDLFreeSurface on a surface which was the screen surface! sdl docu

"The surface returned is freed by SDL_Quit and should not be freed by the caller."
“Always programm as if the person who will be maintaining your program is a violent psychopath that knows where you live”
ok thanks alot! wasnt aware of that =)

Unfortunatly this doesnt fix my problem

EDIT:
It appears that it was my memory logger was causing the error (some how) Its never failed me in the past, ill have to look into it.

thx again for all the help

[Edited by - raptorstrike on August 27, 2005 3:08:28 PM]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
As far as I can tell you are never allocating memory for Curr_Map, but you are deleting it in the GFX destructor. At some point you need to be doing this:
 Curr_Map = new Map;// sometime before you calldelete Curr_Map;

This topic is closed to new replies.

Advertisement