I know there are scores of questions with "* glibc detected * free(): invalid pointer", but the following is a minimalist example with boost::ptr_map:
#include <boost/ptr_container/ptr_map.hpp>
#include <string>
int main() {
boost::ptr_map<std::string, int> map;
int one = 1;
int* pone = &one;
string un = "one";
map.insert(un, pone);
}
The code compiles, but at run time:
boost.library/ptr_container $ ./run.ptrmap
*** glibc detected *** ./run.ptrmap: free(): invalid pointer: 0x00007ffffd5c4578 ***
======= Backtrace: =========
/lib/libc.so.6[0x7fd5e99202f6]
/lib/libc.so.6(cfree+0x6c)[0x7fd5e9924c6c]
./run.ptrmap[0x401066]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7fd5e98c8abd]
./run.ptrmap[0x400e79]
======= Memory map: ========
etc..
It's been repeated throughout SO that this kind of error is caused when trying to delete an invalid pointer. However, what changes in the above very simple code would avert this error?
The background of this question is that I am struggling to understand the proper usage of boost::ptr_map. Unfortunately, the official boost documentation is very sparse as far as ptr_map is concerned (no tutorial, no example with ptr_map). No boost::ptr_map tutorial seem to exist on the web. I have problems retrieving values, and accessing methods to the mapped classes I created. Thus, I tried to create the simplest example I could, but got off on a bad start as evidenced above.
Any help with the glibc error above and with ptr_map in general is appreciated. Thanks.
Edit:
I have read over 20 times, studied and copied the examples from the official boost tutorial http://www.boost.org/doc/libs/1_46_0/libs/ptr_container/doc/tutorial.html
As stated above, the boost tutorial barely covers ptr_map, so linking to it won't make any bit of difference. Basically, I have gone through and through the whole boost ptr container manual many times.
Tutorial: http://www.boost.org/doc/libs/1_39_0/libs/ptr_container/doc/tutorial.html (found by googling for: boost ptr_map)
ptr_map takes ownership of the pointers it's given, therefore it needs to be given heap allocated (new) objects. You've given it a pointer to a stack-allocated object, so when the ptr_map is cleaned up it tries to delete something that is on the stack.
Related
I wanna add custom helloworld syscall to FreeBSD. I used following link as my guide: http://members.tripod.com/s_mathur/bsdhowto.html In step 4 says: Modify the Make File to include sys_hello.c , etc and recompile the kernel! Which Make File? Where is it? and how to compile it and how call syscall hello?
The error that I faced with it, is:
init_sysent.o:(.data + 0x6638): undefined reference to 'sys_hello'
I think that it is because of my Make file, because I don't know I should modify which Make File.
I'm afraid you are not ready to do any kernel development and as such strongly suggest you refrain from it.
I don't know how you ended up on that guide, I have trouble finding it when I look for ways to add system calls to the FreeBSD kernel.
The guide has bits which are outdated and some which were always wrong.
You created a new file (sys_hello.c) but did not add it to the build process. Figuring out how to do that should be trivial.
1. pick a syscall which is always provided, like fork
2. find the file implementing it
3. grep the source tree for mentions of that file
4. profit
Performing the steps and getting the answer is left as an exercise for the reader.
int syshello(p, uap)
struct proc* p; struct syshello_args uap;
K&R C declaration? Just how old is this?
The first argument for several years now is struct thread *.
{
sprintf(uap->buf,"Hello"); /* fill the buffer with Hello */
Fundamentally wrong. Consider what happens if userspace passes a kernel address. Also this assumes shared address spaces to "work". The code should have used copyout. Except the code is additionally wrong by not having an argument allowing the userspace to say what the size is.
p->p_retval[0] = 0; /* set the return value of the system call*/
return 0;
}
As noted earlier, given your difficulty with figuring out what to do with the new file it is clear you are new to programming and as such you really should not touch the kernel until you grow.
While browsing the kernel code, I came accross a keyword that is used in several kernel init functions, __init_refok.
some of the lines I came accross are like
void __init_refok free_initmem(void)
static void __init_refok vgacon_scrollback_startup(void)
const struct linux_logo * __init_refok fb_find_logo(int depth)
void noinline __init_refok rest_init(void)
and others.
I searched for the reference , from that I came to know that it is defined as a preprocessor macro in include/linux/init.h, line 71.
After browsing that, I got the following codes
#define __init_refok __ref
and
#define __ref __section(.ref.text) noinline
After that, I am losing track.
If anyone can let me know what is the purpose of using that keyword in the code, it will be very helpful.
[I am looking for the basic functionality achieved by using this keyword, just like using __init helps to put the initialization code in seperate memory location to be cleared after init process has been completed.]
Thanks in advance.
EDIT
In the include/linux/init.h, it is mentioned like __init_refok is to supress the warning from modpost check, due to any reference form normal code to init section code, but still, I am not getting it exactly. Does that mean that these codes will ba place somewhere else? How actually the behaviour differs from the normal behaviour by using __init_refok keyword?
To my understanding, include/linux/init.h clearly documents the purpose of __init_refok. As you have mentioned
using __init helps to put the initialization code in separate memory
location to be cleared after init process has been completed.
compiler generates a warning when we use the data or code from the separate memory as they might will be removed at the time of the execution of the particular code referencing them.
__init_refok is a way to tell the compiler that you are aware and consciously referencing the initialization code or data. It means ref erencing init section is ok for you. Thus compiler does not generate any warning.
The file also documents that, though the warning is suppressed, it is the programmer's responsibility write such code that refers init section data or code.
of course, no warning does not mean code is correct, so optimally
document why the __ref is needed and why it's OK
In your example, the functions free_initmem(void) is probably referring to some data or code, that are tagged with _init.
The _init_refok tag does not remove the code neither relocate. The code is treated as ordinary except, if it contains any reference to init code or data, warning will be suppressed.
I'm working on some old source code for an embedded system on an m68k target, and I'm seeing massive memory allocation requests sometimes when calling gcvtf to format a floating point number for display. I can probably work around this by writing my own substitute routine, but the nature of the error has me very curious, because it only occurs when the heap starts at or above a certain address, and it goes away if I hack the .ld linker script or remove any set of global variables (which are placed before the heap in my memory map) that add up to enough byte size so that the heap starts below the mysterious critical address.
So, I thought I'd look in the gcc source code for the compiler version I'm using (m68k-elf-gcc 3.3.2). I downloaded what appears to be the source for this version at http://gcc.petsads.us/releases/gcc-3.3.2/, but I can't find the definition for gcvt or gcvtf anywhere in there. When I search for it, grep only finds some documentation and .h references, but not the definition:
$ find | xargs grep gcvt
./gcc/doc/gcc.info: C library functions `ecvt', `fcvt' and `gcvt'. Given va
lid
./gcc/doc/trouble.texi:library functions #code{ecvt}, #code{fcvt} and #code{gcvt
}. Given valid
./gcc/sys-protos.h:extern char * gcvt(double, int, char *);
So, where is this function actually defined in the source code? Or did I download the entirely wrong thing?
I don't want to change this project to use the most recent gcc, due to project stability and testing considerations, and like I said, I can work around this by writing my own formatting routine, but this behavior is very confusing to me, and it will grind my brain if I don't find out why it's acting so weird.
Wallyk is correct that this is defined in the C library rather than the compiler. However, the GNU C library is (nearly always) only used with Linux compilers and distributions. Your compiler, being a "bare-metal" compiler, almost certainly uses the Newlib C library instead.
The main website for Newlib is here: http://sourceware.org/newlib/, and this particular function is defined in the newlib/libc/stdlib/efgcvt.c file. The sources have been quite stable for a long time, so (unless this is a result of a bug) chances are pretty good that the current sources are not too different from what your compiler is using.
As with the GNU C source, I don't see anything in there that would obviously cause this weirdness that you're seeing, but it's all eventually a bunch of wrappers around the basic sprintf routines.
It is in the GNU C library as glibc/misc/efgcvt.c. To save you some trouble, the code for the function is:
char *
__APPEND (FUNC_PREFIX, gcvt) (value, ndigit, buf)
FLOAT_TYPE value;
int ndigit;
char *buf;
{
sprintf (buf, "%.*" FLOAT_FMT_FLAG "g", MIN (ndigit, NDIGIT_MAX), value);
return buf;
}
The directions for obtain glibc are here.
I need to port snprintf() to another platform that does not fully support GLibC.
I am looking for the underlying declaration in the Glibc 2.14 source code. I follow many function calls, but get stuck on vfprintf(). It then seems to call _IO_vfprintf(), but I cannot find the definition. Probably a macro is obfuscating things.
I need to see the real C code that scans the format string and calculates the number of bytes it would write if input buffer was large enough.
I also tried looking in newlib 1.19.0, but I got stuck on _svfprintf_r(). I cannot find the definition anywhere.
Can someone point me to either definition or another one for snprintf()?
I've spent quite a while digging the sources to find _svfprintf_r() (and friends) definitions in the Newlib. Since OP asked about it, I'll post my finding for the poor souls who need those as well. The following holds true for Newlib 1.20.0, but I guess it is more or less the same across different versions.
The actual sources are located in the vfprintf.c file. There is a macro _VFPRINTF_R set to one of _svfiprintf_r, _vfiprintf_r, _svfprintf_r, or _vfprintf_r (depending on the build options), and then the actual implementation function is defined accordingly:
int
_DEFUN(_VFPRINTF_R, (data, fp, fmt0, ap),
struct _reent *data _AND
FILE * fp _AND
_CONST char *fmt0 _AND
va_list ap)
{
...
http://www.ijs.si/software/snprintf/ has what they claim is a portable implementation of snprintf, including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf. Perhaps it can help.
The source code of the GNU C library (glibc) is hosted on sourceware.org.
Here is a link to the implementation of vfprintf(), which is called by snprintf():
https://sourceware.org/git/?p=glibc.git;a=blob;f=stdio-common/vfprintf.c
Hi i'm trying to call the CreateFileA win32 function from a pyx file (cython file)(windows.h is already included from the pxd file), but it doesn't work ... does anyone ever tried to do so... needs help please
More informations:
i got no errors when compiling with Mingw, but at the execution i get -1 as return value..
illustration :
myfile.pxd
cdef extern from "ftd2xx.h":
stuff....
# CreateFileA declaration
HANDLE **CreateFileA***(LPCSTR lpFileName, DWORD dwDesiredAccess,
DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile)*
myfile.pyx
cimport myfile.pxd
def somefuction()
HANDLE a = myfile.**CreateFileA**(......)
at the execution i get -1
Negative 1 (-1 return) means there were multiple errors. It's possible that you are including from the wrong place.
Anyway, my python experience tells me that you can use win32all, from Mark Hammond, to call the win32api. That should solve all your problems.
If it doesn't:
I snooped around Stack Overflow and found lots of people had similar problems. Here are some things you can try:
mingw_setup_args={'options': {'build_ext': {'compiler': 'mingw32'}}}
import pyximport; pyximport.install(setup_args=mingw_setup_args)
Browse to: c:\Python2x\Lib\distutils\distutils.cfg:
[build]
compiler = mingw32
[build_ext]
compiler = mingw32
I encourage you to branch out. If you notice the right sidebar, "related" div on this page, you'll see that there are many people who have asked similar questions. I hope it gets you started. Alternatively, I recommend you look for a better compiler--something that will give you a full error report. Post the error report here. Try to be as precise as possible if there's anything you want to exclude, so that you remove as little helpful data as possible. You can definitely find a solution if one exists, provided you find a complete error message.
I'm almost certain that a full error report exists somewhere, and you just don't know where to look. I found out very early in my programming career that this is always the case when it seems there's "no error" to a programmer: they're looking in the wrong place. So, you should figure out where to look for a complete error report.