LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-08-2012, 05:46 AM   #1
samjam
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Rep: Reputation: 0
gcc 4.6.3 incorrectly detects free() of non-heap object


This gcc code:

Code:
void* d __attribute__((cleanup(free))) = malloc(size);

gives this error:

Code:
attempt to free a non-heap object ‘d’ [-Werror]
It seems like d is a heap object, so is the error with me or gcc 4.6.3?

This works however,

Code:
void *d  = malloc(size);
free(d);
Which makes me think this is a gcc error

I can't find a simple pragma to disable this check; this fails:
Code:
#pragma GCC diagnostic ignored "-Wno-free-nonheap-object"

Last edited by samjam; 10-08-2012 at 06:02 AM. Reason: fix gcc version in title
 
Old 10-08-2012, 06:09 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
From the GCC documentation on the cleanup attribute:

Quote:
The function must take one parameter, a pointer to a type compatible with the variable.
Thus, you need to create your own function that takes a void**. With this short example, GCC does not complain (and valgrind says it's happy):

Code:
#include <stdlib.h>

void free_ptr(void **ptr)
{
    free(*ptr);
}

int main(void)
{
    void *d __attribute((cleanup(free_ptr))) = malloc(20);
}
Edit:

So your original problem was that &d was being passed to free(), which of course bad.

Last edited by JohnGraham; 10-08-2012 at 06:11 AM.
 
Old 10-08-2012, 06:25 AM   #3
samjam
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by JohnGraham View Post
From the GCC documentation on the cleanup attribute:

Thus, you need to create your own function that takes a void**. With this short example, GCC does not complain (and valgrind says it's happy):
Thank-you. I'm sure I knew that once. Much appreciated.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Aide Detects Added New File Incorrectly dman777 Linux - Security 5 09-24-2012 07:15 PM
gcc 4.4.1 incorrectly detects free() of non-heap object Skaperen Programming 13 09-16-2010 06:20 AM
ntfsresize detects device size incorrectly ternarybit Linux - Software 1 08-25-2009 11:50 AM
CPP: Multiple pointers to one heap object problem. RHLinuxGUY Programming 2 03-27-2008 09:01 AM
Linux (FC4) Incorrectly Detects UDMA Mode on Asus DVD E616P2 gillius Linux - Hardware 1 07-13-2005 09:35 PM


All times are GMT -5. The time now is 04:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration