Invalid Write of Size 8 at first line in class constructor

Go To StackoverFlow.com

4

I'm having trouble getting a simple class constructor to work.

// In XModule.h
class XModule
{
...
public:
  TXMHeader     header;     // module header
  TXMInstrument*    instr;      // all instruments (256 of them)
  TXMSample*        smp;        // all samples (256 of them, only 255 can be used)
  TXMPattern*       phead;      // all pattern headers (256 of them)
}

Module.cpp

// In XModule.cpp
....
XModule::XModule()
{
  // allocated necessary space for all possible patterns, instruments and samples
  phead = new TXMPattern[256]; // Line # 1882
  instr = new TXMInstrument[256];
  smp = new TXMSample[MP_MAXSAMPLES];

  memset(&header,0,sizeof(TXMHeader));

  if (instr)
    memset(instr,0,sizeof(TXMInstrument)*256);

  if (smp)
    memset(smp,0,sizeof(TXMSample)*MP_MAXSAMPLES);

  if (phead)
    memset(phead,0,sizeof(TXMPattern)*256);

}
....

Extractor.cpp

#include "Extractor.h"
#include "XModule.h"

#include <iostream>
using namespace std;

int main ()
{
  XModule* module = new XModule();
  SYSCHAR* fileName = "Greensleeves.xm";

  ...

  return 0;
}

When I run with valgrind I get the following error:

==21606== Invalid write of size 8
==21606==    at 0x408BD3: XModule::XModule() (XModule.cpp:1882)
==21606==    by 0x4012D8: main (Extractor.cpp:9)
==21606==  Address 0x64874f0 is not stack'd, malloc'd or (recently) free'd

The later in the line memset(instr,0,sizeof(TXMInstrument)*256); it zeroes out phead, instr and smp.

Stepping through with gdb revealed that phead, instr, and smp are set correctly, before that, but the addresses of the array pointers are within the area that new allocated for the instr array. Examining &phead revealed this to be true.

Why does new the call to instr = new TXMInstrument[256]; assign memory space that is used for phead, instr and smp and what can I do to fix this or further diagnose the issue?

2012-04-04 23:43
by Daniel X Moore
Although your code is really inadvisable, poor C++ in almost every line, it isn't wrong as such. The error must be elsewhere. What is line 1882 - Kerrek SB 2012-04-04 23:47
I would also speculate that you're class is being constructed in invalid memory, possibly one past the end of an array of XModule objects. That would explain your symptoms. Show us the code near main (Extractor.cpp:9)Mooing Duck 2012-04-04 23:50
A write of size 8 implies that it's the assignment to phead that's invalid (i.e. the class itself is in invalid memory). Can you provide a complete test case - Oliver Charlesworth 2012-04-04 23:50
Here are links to the full source files: https://github.com/STRd6/Milky-Tracker/blob/49b0a897a731bb898ee545272d4998cb62e1523d/src/milkyplay/XModule.h

https://github.com/STRd6/Milky-Tracker/blob/49b0a897a731bb898ee545272d4998cb62e1523d/src/milkyplay/XModule.cpp

The project is MilkyTracker, a tool for creating sample based audio files. I'm trying to use a component of it to load XM files and extract data - Daniel X Moore 2012-04-04 23:52

Link to full file Extractor.cpp: https://gist.github.com/230666 - Daniel X Moore 2012-04-04 23:54
@DanielXMoore: By "complete test case", I don't really mean "all of your original code", no one here is going to trawl through thousands of lines of code. I mean a minimal test case - Oliver Charlesworth 2012-04-04 23:56
@DanielXMoore: The places I thought to look are fine, I'm stumped. You'll have to do a SSCCE/minimal testcase - Mooing Duck 2012-04-04 23:56
Ok, working on a minimal test case, may take a while because the project is pretty large and I'm unfamiliar with it. Any advice on extracting code for a minimal test case from large open source projects - Daniel X Moore 2012-04-05 00:01
I've deleted half of XModule.cpp, reducing it to mostly what is described here, and it is not exhibiting the error. I'll keep adding pieces in until I pinpoint it. Thanks for the help thus far - Daniel X Moore 2012-04-05 00:29


4

It turns out there were a bunch of #IFDEFs in the class definition, so when I was compiling my utility against the library built with the projects makefile it was using the source headers and thought the class had a different amount of properties, so they were not arranged in memory correctly and got crushed by the allocation of the arrays.

I solved it by not using the projects library, copying the source files to a new folder, and running g++ *.cpp.

2012-04-06 05:01
by Daniel X Moore
Ads