Stacking multiple shields allows only partial use of SD logger funcionality

Hello,

I am using this SD shield, with this ethernet shield, on top of this Arduino UNO R2.

I directly connected the SD shield on top of the arduino shield. Two unconnected terminals are present on each top side (labelled SCL, SDA on the right, and IOREF on the left), and I manually connected the SD shield to the ethernet shield (pins RST through GND on the upper left side (if the USB connector is pointing up), and pins 11-13 on the right side), with an extra connection from pin 8 on the SD shield to the pin 10 on the ethernet shield.

The Serial monitor shows me this after I upload the code, which to me looks like the SD card can be initialized, and the file can be checked if it exists, but it will not open.

[backSoon]
IP:  192.168.7.102
GW:  192.168.7.1
DNS: 192.168.7.1
Card initialized.
File exists.
File cannot be opened.
File exists.
File cannot be opened.

If I upload the vanilla SD card filedump (using pin 10 instead of 4), the file contents are dumped correctly, and the ethernet shield is ignored. If I use only the EtherCard "backSoon" example, the board also works fine. So both cards work fine independently with the current wiring, which makes me think the problem is in the software somehow.

As for the real code I am using, it can be checked here:

// Present a "Will be back soon web page", as stand-in webserver.
// 2011-01-30 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
 
#include <EtherCard.h>
#include <SD.h>

#define STATIC 0  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,7,15 };
// gateway ip address
static byte gwip[] = { 192,168,7,1 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
  "<head><title>"
    "Service Temporarily Unavailable"
  "</title></head>"
  "<body>"
    "<h3>This service is currently unavailable</h3>"
    "<p><em>"
      "The main server is currently off-line.
"
      "Please try again later."
    "</em></p>"
  "</body>"
"</html>"
;

void setup(){
  Serial.begin(57600);
  Serial.println("\n[backSoon]");
  
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
#if STATIC
  ether.staticSetup(myip, gwip);
#else
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
#endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);
  
  if (!SD.begin(10)) {
    Serial.println("Card failed, or not present");
  }
  else
  {
    Serial.println("Card initialized.");
  }
}

void loop(){
  // wait for an incoming TCP packet, but ignore its contents
  if (ether.packetLoop(ether.packetReceive())) {
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
    if(SD.exists("DATALOG.TXT")) Serial.println("File exists.");
    File dataFile = SD.open("datalog.txt");
    if (dataFile)
    {
      Serial.println("File opened.");
      dataFile.close();
    }
    else
    {
      Serial.println("File cannot be opened.");
    }
  }
}

Attached, only for curiosity's sake, pictures of the real thing.

Now question: Is stacking the shields the issue here? Could the SPI management of one of the boards need some fine tuning or the adding of some graceful closures to allow multiple uses of it? Is it possible that one of these shields just does not play nice with other boards? Am I missing something using the SD card?

Well, after digging a little bit further, I learned that the problem happened in line 24 of the File.cpp that comes with the SD examples.

I changed the malloc to realloc, and it works fine now.