Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - cleaning up an img_t* in C++: (6 Items)
   
cleaning up an img_t* in C++  
So, i've been working with the IMG library in C++ and I'm working on closing down memory leaks.

The way i've been creating my img_t objects is:

img_t* OrigImage = new img_t();

This works well enough until I start doing my house cleaning:

if (OrigImage->access.direct.data != NULL) {
	delete OrigImage->access.direct.data;
}
if (OrigImage->palette != NULL) {
	delete OrigImage->palette;
}
delete OrigImage;

This code works pretty good, except when there's palette information...and then it crashes.

So, anybody know a better way to fully clean up an img_t* ?
RE: cleaning up an img_t* in C++  
try this:

if (img->flags & IMG_DIRECT) {
    free(img->access.direct.data);
    img->flags &= ~IMG_DIRECT;
    if (img->flags & IMG_PALETTE) {
        img->flags &= ~IMG_PALETTE;
    }
} else if (img->flags & IMG_PALETTE) {
    free(img->palette);
    img->flags &= ~IMG_PALETTE;
}

-----Original Message-----
From: Michael Briggs [mailto:community-noreply@qnx.com] 
Sent: February 23, 2011 1:09 PM
To: advanced-graphics
Subject: cleaning up an img_t* in C++

So, i've been working with the IMG library in C++ and I'm working on
closing down memory leaks.

The way i've been creating my img_t objects is:

img_t* OrigImage = new img_t();

This works well enough until I start doing my house cleaning:

if (OrigImage->access.direct.data != NULL) {
	delete OrigImage->access.direct.data;
}
if (OrigImage->palette != NULL) {
	delete OrigImage->palette;
}
delete OrigImage;

This code works pretty good, except when there's palette
information...and then it crashes.

So, anybody know a better way to fully clean up an img_t* ?



_______________________________________________

Advanced Graphics
http://community.qnx.com/sf/go/post83429
RE: cleaning up an img_t* in C++  
Derek -

Thanks for the sample.  It worked perfectly...and helped demonstrate my lack of deep understanding of the img_t 
structure. :-)

I made a cleanup function that passes in an img_t* item, runs your code, then deletes the img_t* item.

- Mike

-----Original Message-----
From: Derek Leach [mailto:community-noreply@qnx.com] 
Sent: Wednesday, February 23, 2011 1:20 PM
To: advanced-graphics
Subject: RE: cleaning up an img_t* in C++

try this:

if (img->flags & IMG_DIRECT) {
    free(img->access.direct.data);
    img->flags &= ~IMG_DIRECT;
    if (img->flags & IMG_PALETTE) {
        img->flags &= ~IMG_PALETTE;
    }
} else if (img->flags & IMG_PALETTE) {
    free(img->palette);
    img->flags &= ~IMG_PALETTE;
}

-----Original Message-----
From: Michael Briggs [mailto:community-noreply@qnx.com] 
Sent: February 23, 2011 1:09 PM
To: advanced-graphics
Subject: cleaning up an img_t* in C++

So, i've been working with the IMG library in C++ and I'm working on
closing down memory leaks.

The way i've been creating my img_t objects is:

img_t* OrigImage = new img_t();

This works well enough until I start doing my house cleaning:

if (OrigImage->access.direct.data != NULL) {
	delete OrigImage->access.direct.data;
}
if (OrigImage->palette != NULL) {
	delete OrigImage->palette;
}
delete OrigImage;

This code works pretty good, except when there's palette
information...and then it crashes.

So, anybody know a better way to fully clean up an img_t* ?



_______________________________________________

Advanced Graphics
http://community.qnx.com/sf/go/post83429




_______________________________________________

Advanced Graphics
http://community.qnx.com/sf/go/post83432
Re: RE: cleaning up an img_t* in C++  
Hi Derek,

I have a question with regard to img_t but in regard to who is allocating memory for access.direct.data pointer when 
using setting the IMG_DIRECT flag?  Will  the img_load_file() allocate memory for it?

I am trying to read a .png file, make some modification to the file and then save it as new .png file.  However, when I 
set the IMG_DIRECT flag, my  system hangs.  If I don't set the IMG_DIRECT flag, the file is loaded correctly.

I am using QNX 6.4.1 on the host and the same version running on a VM target.

I appreciate it if you can give me pointer on where the problem could be.

Thanks,

RS.
RE: cleaning up an img_t* in C++  
You cannot mix delete and malloc. Also in C++ it`s legal to do a delete on a NULL, hence you can get rid of the ifs.


> -----Message d'origine-----
> De : Michael Briggs [mailto:community-noreply@qnx.com]
> Envoyé : 23 février 2011 13:09
> À : advanced-graphics
> Objet : cleaning up an img_t* in C++
> 
> So, i've been working with the IMG library in C++ and I'm working on closing
> down memory leaks.
> 
> The way i've been creating my img_t objects is:
> 
> img_t* OrigImage = new img_t();
> 
> This works well enough until I start doing my house cleaning:
> 
> if (OrigImage->access.direct.data != NULL) {
> 	delete OrigImage->access.direct.data;
> }
> if (OrigImage->palette != NULL) {
> 	delete OrigImage->palette;
> }
> delete OrigImage;
> 
> This code works pretty good, except when there's palette information...and
> then it crashes.
> 
> So, anybody know a better way to fully clean up an img_t* ?
> 
> 
> 
> _______________________________________________
> 
> Advanced Graphics
> http://community.qnx.com/sf/go/post83429
> 
RE: cleaning up an img_t* in C++  
Mario -

While, yes, it is syntactically legal to do a delete on a NULL, in QNX on ARM-LE v7 it generates a core dump of the file
 due to being an illegal function.  So that's why I was protecting against NULLs.

However, Derek's solution was much more elegant and does the job just fine.

- Mike

-----Original Message-----
From: Mario Charest [mailto:community-noreply@qnx.com] 
Sent: Wednesday, February 23, 2011 2:17 PM
To: advanced-graphics
Subject: RE: cleaning up an img_t* in C++

You cannot mix delete and malloc. Also in C++ it`s legal to do a delete on a NULL, hence you can get rid of the ifs.


> -----Message d'origine-----
> De : Michael Briggs [mailto:community-noreply@qnx.com]
> Envoyé : 23 février 2011 13:09
> À : advanced-graphics
> Objet : cleaning up an img_t* in C++
> 
> So, i've been working with the IMG library in C++ and I'm working on closing
> down memory leaks.
> 
> The way i've been creating my img_t objects is:
> 
> img_t* OrigImage = new img_t();
> 
> This works well enough until I start doing my house cleaning:
> 
> if (OrigImage->access.direct.data != NULL) {
> 	delete OrigImage->access.direct.data;
> }
> if (OrigImage->palette != NULL) {
> 	delete OrigImage->palette;
> }
> delete OrigImage;
> 
> This code works pretty good, except when there's palette information...and
> then it crashes.
> 
> So, anybody know a better way to fully clean up an img_t* ?
> 
> 
> 
> _______________________________________________
> 
> Advanced Graphics
> http://community.qnx.com/sf/go/post83429
> 




_______________________________________________

Advanced Graphics
http://community.qnx.com/sf/go/post83439